package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.ShShopMapper;
import com.bofeng.dao.ShShopMatMapper;
import com.bofeng.entity.ShShopMat;
import lombok.var;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ShShopMatService {

    @Autowired
    private ShShopMapper shShopMapper;
    @Autowired
    private ShShopMatMapper shShopMatMapper;

//    public List<QzTask> selectAll () {
//        return qzTaskDao.selectAll();
//    }
//
//    public List<QzTask> queryByHouseNumber (String houseNumber, Integer status) {
//        return qzTaskDao.queryByHouseNumber(houseNumber, status);
//    }
//
//    public QzTask queryByTaskId (Long taskId) {
//        return qzTaskDao.queryByTaskId(taskId);
//    }

    //商品的新增
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int addShShopMat(Long userId, String matName, Integer matNum) throws Exception {
        DateTime dateTime = new DateTime();

        var shShopDb = shShopMapper.selectById(userId);
        if (null == shShopDb) {
            throw new Exception("发布失败");
        }

        var mat = new ShShopMat();
        mat.setMatId(IdWorker.getId());
        mat.setShopId(shShopDb.getShopId());
        mat.setMatName(matName);
        mat.setMatNum(matNum);
        mat.setStatus(1);

    /*long userId = Long.parseLong(JwtHelper.getUserId());
    qzTak.setUserId(userId);
    qzTak.setUserCreate(userId);*/
        mat.setTimeCreate(dateTime);
        /* qzTak.setUserUpdate(userId);*/
        mat.setTimeUpdate(dateTime);
        return shShopMatMapper.insert(mat);
    }


    //商品的修改
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int editShShopMat(Long matId, String matName, Integer matNum) throws Exception {
        DateTime dateTime = new DateTime();

//        var shShopDb = shShopMapper.selectById(userId);
//        if (null == shShopDb) {
//            throw new Exception("发布失败");
//        }

        var shShopMatDb = shShopMatMapper.selectById(matId);
        if (null == shShopMatDb) {
            throw new Exception("修改失败");
        }

        shShopMatDb.setMatName(matName);
        shShopMatDb.setMatNum(matNum);
        shShopMatDb.setStatus(1);

    /*long userId = Long.parseLong(JwtHelper.getUserId());
    qzTak.setUserId(userId);
    qzTak.setUserCreate(userId);*/
//        mat.setTimeCreate(dateTime);
        /* qzTak.setUserUpdate(userId);*/
        shShopMatDb.setTimeUpdate(dateTime);
        return shShopMatMapper.updateById(shShopMatDb);
    }

    //商品的置顶2、下架0修改
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int editShShopMatStatus(Long matId, Integer status) throws Exception {
        DateTime dateTime = new DateTime();

//        var shShopDb = shShopMapper.selectById(userId);
//        if (null == shShopDb) {
//            throw new Exception("发布失败");
//        }

        var shShopMatDb = shShopMatMapper.selectById(matId);
        if (null == shShopMatDb) {
            throw new Exception("修改失败");
        }

        shShopMatDb.setStatus(status);

    /*long userId = Long.parseLong(JwtHelper.getUserId());
    qzTak.setUserId(userId);
    qzTak.setUserCreate(userId);*/
//        mat.setTimeCreate(dateTime);
        /* qzTak.setUserUpdate(userId);*/
        shShopMatDb.setTimeUpdate(dateTime);
        return shShopMatMapper.updateById(shShopMatDb);
    }


    public List<ShShopMat> queryMyShShopMat(Long userId) throws Exception {
        var shShopDb = shShopMapper.selectById(userId);
        if (null == shShopDb) {
            throw new Exception("获取失败");
        }
        return shShopMatMapper.queryMyShShopMat(shShopDb.getShopId());
    }

    public List<ShShopMat> queryAllShShopMat() throws Exception {
        return shShopMatMapper.queryAllShShopMat();
    }
}
