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.WarehouseConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.model.pojo.mst.Warehouse;
import com.dk.mdm.mapper.mst.WarehouseMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.WarehouseQuery;
import com.dk.mdm.model.response.mst.WarehouseResponse;
import com.dk.mdm.model.vo.mst.WarehouseVO;
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 java.util.Map;

@Service
@Transactional
public class WarehouseService extends BaseService<Warehouse> {

    @Override
    public BaseMapper<Warehouse> getRepository() {
        return warehouseMapper;
    }

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private WarehouseConvert warehouseConvert;


    @Autowired
    private AuthUtils authUtils;

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


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


    /**
     * @desc : 新建
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(WarehouseVO warehouseVO) {
        //实体转换
        Warehouse warehouse = warehouseConvert.convertToPo(warehouseVO);
        //设置序号
        warehouse.setDisplayNo(commonService.getMaxDisplayNo(Constant.DisplayNoTable.WAREHOUSE));
        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.WAREHOUSE.getName(), true);
        warehouse.setWhId(uniqueNoteCode.get("outId").toString());
        warehouse.setWhCode(uniqueNoteCode.get("outNote").toString());
        //新建如果是默认取消其他默认
        if(warehouseVO.getFlgDefault() != null ){
            //默认  把其他置为非默认
            warehouseMapper.updateFlgDefault(authUtils.getStaff().getCpId());
        }
        //新建
        warehouseMapper.insert(warehouse);
        return ResponseResultUtil.success();
    }


    /**
     * @desc : 编辑
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> update(WarehouseVO warehouseVO) {
        Warehouse warehouse1 = warehouseMapper.selectById(warehouseVO.getWhId());
        if ((warehouse1.getFlgDefault() == null || !warehouse1.getFlgDefault()) && warehouseVO.getFlgDefault() != null && warehouseVO.getFlgDefault()) {
            //默认  把其他置为非默认
            warehouseMapper.updateFlgDefault(warehouseVO.getCpId());
        }
        Warehouse warehouse = warehouseConvert.convertToPo(warehouseVO);
        super.updateByUuid(warehouse);
        return ResponseResultUtil.success();
    }



    /**
     * @desc : 编辑
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> updateBatchWarehouseDisplayNo(WarehouseVO warehouseVO) {
        warehouseMapper.updateBatchWarehouseDisplayNo(warehouseVO.getDataIds());
        return ResponseResultUtil.success();
    }

}
