package com.zt.plat.module.qms.resource.material.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.business.config.service.ConfigWarehouseLocationService;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInfomationDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryCheckDetailDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryCheckItemDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialLifecycleDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialInventoryCheckDetailMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 库存盘点明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialInventoryCheckDetailServiceImpl implements MaterialInventoryCheckDetailService {

    @Resource
    private MaterialInventoryCheckDetailMapper materialInventoryCheckDetailMapper;

    @Transactional
    @Override
    public MaterialInventoryCheckDetailRespVO createMaterialInventoryCheckDetail(MaterialInventoryCheckDetailSaveReqVO createReqVO) {
        MaterialInventoryCheckDetailDO checkDetailDO = BeanUtils.toBean(createReqVO, MaterialInventoryCheckDetailDO.class);
        // 插入
        materialInventoryCheckDetailMapper.insert(checkDetailDO);
        // 返回
        return BeanUtils.toBean(checkDetailDO, MaterialInventoryCheckDetailRespVO.class);
    }

    @Override
    public void updateMaterialInventoryCheckDetail(MaterialInventoryCheckDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialInventoryCheckDetailExists(updateReqVO.getId());
        // 更新
        MaterialInventoryCheckDetailDO updateObj = BeanUtils.toBean(updateReqVO, MaterialInventoryCheckDetailDO.class);
        materialInventoryCheckDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialInventoryCheckDetail(Long id) {
        // 校验存在
        validateMaterialInventoryCheckDetailExists(id);
        // 删除
        materialInventoryCheckDetailMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialInventoryCheckDetailListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialInventoryCheckDetailExists(ids);
        // 删除
        materialInventoryCheckDetailMapper.deleteByIds(ids);
        }

    private void validateMaterialInventoryCheckDetailExists(List<Long> ids) {
        List<MaterialInventoryCheckDetailDO> list = materialInventoryCheckDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_INVENTORY_CHECK_DETAIL_NOT_EXISTS);
        }
    }

    private void validateMaterialInventoryCheckDetailExists(Long id) {
        if (materialInventoryCheckDetailMapper.selectById(id) == null) {
            throw exception(MATERIAL_INVENTORY_CHECK_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialInventoryCheckDetailDO getMaterialInventoryCheckDetail(Long id) {
        return materialInventoryCheckDetailMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialInventoryCheckDetailDO> getMaterialInventoryCheckDetailPage(MaterialInventoryCheckDetailPageReqVO pageReqVO) {
        return materialInventoryCheckDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaterialInventoryCheckDetailDO> getListByItemId(Long itemId) {
        return materialInventoryCheckDetailMapper.selectList(Wrappers.lambdaQuery(MaterialInventoryCheckDetailDO.class)
                .eq(MaterialInventoryCheckDetailDO::getItemId, itemId));
    }

    @Override
    public void addBatch(List<MaterialInventoryCheckDetailDO> checkDetailDOS) {
        if (CollUtil.isEmpty(checkDetailDOS)) return;
        materialInventoryCheckDetailMapper.insertBatch(checkDetailDOS);
    }

    @Override
    public MaterialInventoryCheckDetailRespVO inventoryCheckInfomation(MaterialInventoryCheckDetailSaveReqVO inventoryReqVO) {
        return null;
    }

    @Override
    public PageResult<MaterialInventoryCheckDetailRespVO> getPageWithInf(MaterialInventoryCheckDetailPageReqVO pageReqVO) {
        return materialInventoryCheckDetailMapper.selectPageWithInf(pageReqVO);
    }

    @Override
    public List<MaterialInventoryCheckDetailDO> getListNotAtWarehouse(Long lfcId) {
        return materialInventoryCheckDetailMapper.selectList(Wrappers.lambdaQuery(MaterialInventoryCheckDetailDO.class)
                .eq(MaterialInventoryCheckDetailDO::getCheckId, lfcId)
                .eq(MaterialInventoryCheckDetailDO::getAtWarehouse, 0)
                .eq(MaterialInventoryCheckDetailDO::getPresent, 1));
    }

    @Override
    public List<MaterialInventoryCheckDetailDO> getListNotPresentAndAtWarehouse(Long lfcId) {
        return materialInventoryCheckDetailMapper.selectList(Wrappers.lambdaQuery(MaterialInventoryCheckDetailDO.class)
                .eq(MaterialInventoryCheckDetailDO::getCheckId, lfcId)
                .eq(MaterialInventoryCheckDetailDO::getAtWarehouse, 1)
                .eq(MaterialInventoryCheckDetailDO::getPresent, 0));
    }

    @Override
    public MaterialInventoryCheckDetailDO getByCheckIdAndInfId(Long checkId, Long infId) {
        return materialInventoryCheckDetailMapper.selectOne(Wrappers.lambdaQuery(MaterialInventoryCheckDetailDO.class)
                .eq(MaterialInventoryCheckDetailDO::getCheckId, checkId)
                .eq(MaterialInventoryCheckDetailDO::getInfomationId, infId));
    }

    @Override
    public void save(MaterialInventoryCheckDetailDO checkDetailDO) {
        materialInventoryCheckDetailMapper.insert(checkDetailDO);
    }

    @Override
    public void updateById(MaterialInventoryCheckDetailDO checkDetailDO) {
        materialInventoryCheckDetailMapper.updateById(checkDetailDO);
    }

}