package com.zt.plat.module.erp.service.erp;

import com.zt.plat.framework.common.exception.ErrorCode;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.controller.admin.erp.vo.DeleteErpMaterialCorrVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialCorrRspVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialCorrSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpMaterialCorrDO;
import com.zt.plat.module.erp.dal.mysql.erp.ErpErpMaterialCorrMapper;
import com.zt.plat.module.system.api.dept.DeptApi;
import com.zt.plat.module.system.api.dept.dto.DeptRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
@Validated
public class ErpErpMaterialCorrServiceImpl implements ErpErpMaterialCorrService{
    @Resource
    private ErpErpMaterialCorrMapper erpErpMaterialCorrMapper;
    @Resource
    private DeptApi deptApi;

    @Override
    public List<ErpMaterialCorrRspVO> create(ErpMaterialCorrSaveReqVO reqVO) {
        CommonResult<DeptRespDTO> dept = deptApi.getDept(reqVO.getDeptId());
        DeptRespDTO data = dept.getData();
        if (data==null||data.getCode().isEmpty()) {
            throw exception(new ErrorCode(1027004000, "部门或部门公司编码不存在，部门编号为:{}"), reqVO.getDeptId().toString());
        }
        List<ErpMaterialCorrDO> erpMaterialCorrDOS=new ArrayList<>();
        reqVO.getMaterials().forEach(materials -> {
            //判断是否存在
            if (erpErpMaterialCorrMapper.selectOne(
                    new LambdaQueryWrapperX<ErpMaterialCorrDO>()
                            .eq(ErpMaterialCorrDO::getMaterialParentId, reqVO.getMaterialParentId())
                            .eq(ErpMaterialCorrDO::getMaterialId, materials.getMaterialId())
                            .eqIfPresent(ErpMaterialCorrDO::getCompanyCode, data.getCode())
                            .eqIfPresent(ErpMaterialCorrDO::getDeptId, reqVO.getDeptId())
            ) != null) {
                return;
            }
            erpMaterialCorrDOS.add(ErpMaterialCorrDO.builder()
                    .materialParentId(reqVO.getMaterialParentId())
                    .materialParentCode(reqVO.getMaterialParentCode())
                    .materialId(materials.getMaterialId())
                    .deptId(reqVO.getDeptId())
                    .companyCode(data.getCode())
                    .materialCode(materials.getMaterialCode())
                    .build());
        });
        erpErpMaterialCorrMapper.insertBatch(erpMaterialCorrDOS);
        return BeanUtils.toBean(erpMaterialCorrDOS, ErpMaterialCorrRspVO.class);
    }

    @Override
    public void deleteBatch(DeleteErpMaterialCorrVO reqVO) {
        erpErpMaterialCorrMapper.delete(new LambdaQueryWrapperX<ErpMaterialCorrDO>()
                .eq(ErpMaterialCorrDO::getMaterialParentId, reqVO.getMaterialParentId())
                .in(ErpMaterialCorrDO::getMaterialId, reqVO.getMaterialIds()));
    }

    @Override
    public List<ErpMaterialCorrRspVO> getErpMaterialByMainMaterial(List<Long> mainMaterialIds, String companyCode) {
        LambdaQueryWrapperX<ErpMaterialCorrDO> erpMaterialCorrDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        erpMaterialCorrDOLambdaQueryWrapperX.in(ErpMaterialCorrDO::getMaterialParentId, mainMaterialIds);
        erpMaterialCorrDOLambdaQueryWrapperX.eqIfPresent(ErpMaterialCorrDO::getCompanyCode, companyCode);
        return BeanUtils.toBean( erpErpMaterialCorrMapper.selectList(erpMaterialCorrDOLambdaQueryWrapperX), ErpMaterialCorrRspVO.class);
    }

    @Override
    public List<ErpMaterialCorrRspVO> getErpMaterialByMainMaterialByCode(String code, String companyCode) {
        LambdaQueryWrapperX<ErpMaterialCorrDO> erpMaterialCorrDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        erpMaterialCorrDOLambdaQueryWrapperX.eqIfPresent(ErpMaterialCorrDO::getMaterialParentCode, code);
        erpMaterialCorrDOLambdaQueryWrapperX.eqIfPresent(ErpMaterialCorrDO::getCompanyCode, companyCode);
        return BeanUtils.toBean(erpErpMaterialCorrMapper.selectList(erpMaterialCorrDOLambdaQueryWrapperX), ErpMaterialCorrRspVO.class);
    }

    @Override
    public List<ErpMaterialCorrRspVO> getErpMaterialByCompanyCode(String CompanyCode) {
        return BeanUtils.toBean(erpErpMaterialCorrMapper.selectList(ErpMaterialCorrDO::getCompanyCode, CompanyCode), ErpMaterialCorrRspVO.class);
    }
}
