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.GoodsBrandConvert;
import com.dk.mdm.infrastructure.convert.mst.GoodsSkuConvert;
import com.dk.mdm.model.pojo.mst.GoodsBrand;
import com.dk.mdm.mapper.mst.GoodsBrandMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.GoodsSku;
import com.dk.mdm.model.pojo.mst.Role;
import com.dk.mdm.model.query.mst.GoodsSkuQuery;
import com.dk.mdm.model.query.mst.RoleQuery;
import com.dk.mdm.model.response.mst.GoodsSkuResponse;
import com.dk.mdm.model.vo.mst.GoodsSkuVO;
import com.dk.mdm.model.vo.mst.RoleVo;
import com.dk.mdm.service.common.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.dk.mdm.model.query.mst.GoodsBrandQuery;
import com.dk.mdm.model.response.mst.GoodsBrandResponse;
import com.dk.mdm.model.vo.mst.GoodsBrandVO;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class GoodsBrandService extends BaseService<GoodsBrand> {

    @Override
    public BaseMapper<GoodsBrand> getRepository() {
        return goodsBrandMapper;
    }

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;


    @Autowired
    private GoodsBrandConvert goodsBrandConvert;

    @Autowired
    private CommonService commonService;

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

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


    /**
     * @desc : 新建
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(GoodsBrandVO goodsBrandVO) {
        //实体转换
        GoodsBrand goodsBrand = goodsBrandConvert.convertToPo(goodsBrandVO);
        // 品牌简称
        if(goodsBrand.getShortName() == null){
            goodsBrand.setShortName(goodsBrand.getBrandName());
        }
        //设置序号
        goodsBrand.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.GOODSBRAND));
        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.BRAND.getName(), true);
        goodsBrand.setBrandId(uniqueNoteCode.get("outId").toString());
        goodsBrand.setBrandCode(uniqueNoteCode.get("outNote").toString());
        //新建
        goodsBrandMapper.insert(goodsBrand);

        //保存供应商
        List<String> supplierIds = goodsBrandVO.getSupplierIds();
        if (supplierIds != null) {
            for (String supplierId : supplierIds) {
                goodsBrandMapper.saveSupplierBrand(supplierId, goodsBrand.getBrandId());
            }
        }
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 编辑
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> updateGoodsBrand(GoodsBrandVO goodsBrandVO) {
        GoodsBrand goodsBrand = goodsBrandConvert.convertToPo(goodsBrandVO);
        super.updateByUuid(goodsBrand);
        return ResponseResultUtil.success();
    }


}
