package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.GoodsCategoryConvert;
import com.dk.mdm.model.pojo.mst.GoodsBrand;
import com.dk.mdm.model.pojo.mst.GoodsCategory;
import com.dk.mdm.mapper.mst.GoodsCategoryMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.GoodsBrandQuery;
import com.dk.mdm.model.query.mst.GoodsCategoryQuery;
import com.dk.mdm.model.query.mst.GoodsSeriesQuery;
import com.dk.mdm.model.response.mst.GoodsCategoryResponse;
import com.dk.mdm.model.response.mst.GoodsSeriesResponse;
import com.dk.mdm.model.vo.mst.GoodsBrandVO;
import com.dk.mdm.model.vo.mst.GoodsCategoryVO;
import com.dk.mdm.service.common.CommonService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

@Service
@Transactional
public class GoodsCategoryService extends BaseService<GoodsCategory> {

    @Override
    public BaseMapper<GoodsCategory> getRepository() {
        return goodsCategoryMapper;
    }

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    @Autowired
    private GoodsCategoryConvert goodsCategoryConvert;

    @Autowired
    private CommonService commonService;

    /**
     * @desc : 重写主键
     * @author : 于继渤
     * @date : 2024/2/29 20:29
     */
    @Override
    public String getPrimaryKey() {
        return "cat_id";
    }

    /**
     * @desc : 查询
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Pagination
    public ResponseResultVO<PageList<GoodsCategoryResponse>> selectByCond(GoodsCategoryQuery goodsCategoryQuery) {
        return super.mergeListWithCount(goodsCategoryQuery, goodsCategoryMapper.selectByCond(goodsCategoryQuery),
                goodsCategoryMapper.countByCond(goodsCategoryQuery));
    }


    /**
     * @desc : 新建
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(GoodsCategoryVO goodsCategoryVO) {
        //实体转换
        GoodsCategory goodsCategory = goodsCategoryConvert.convertToPo(goodsCategoryVO);
        //设置序号
        goodsCategory.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.GOODSSERIES));
        if (goodsCategory.getParentId() != null) {
            //插入TOPid
            goodsCategory.setTopId(goodsCategory.getParentId());
        }

        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CATEGORY.getName(), true);
        goodsCategory.setCatId(uniqueNoteCode.get("outId").toString());
        goodsCategory.setCatCode(uniqueNoteCode.get("outNote").toString());
        //新建
        goodsCategoryMapper.insert(goodsCategory);
        //执行函数
        goodsCategoryMapper.resetLevelGoodsCategory(new GoodsCategoryQuery().setTopId(goodsCategory.getParentId()).setCpId(goodsCategory.getCpId()));
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 编辑
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> update(GoodsCategoryVO goodsCategoryVO) {
        GoodsCategory goodsCategory = goodsCategoryConvert.convertToPo(goodsCategoryVO);
        super.updateByUuid(goodsCategory);
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 批量启用 根据父级id
     * @author : 于继渤
     * @date : 2024/2/26 10:36
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> enableBachByParentId(String id) {
        return ResponseResultUtil.success(goodsCategoryMapper.enableBachByParentId(id));
    }

    /**
     * @desc : 批量停用 根据父级id
     * @author : 于继渤
     * @date : 2024/2/26 10:36
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> disableBachByParentId(String id) {
        return ResponseResultUtil.success(goodsCategoryMapper.disableBachByParentId(id));
    }
}
