package com.zt.plat.module.base.service.internalsupplyfactory;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.tenant.core.aop.TenantIgnore;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryEnableReqVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryPageReqVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryRespVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactorySaveReqVO;
import com.zt.plat.module.base.dal.dao.internalsupplyfactory.InternalSupplyFactoryMapper;
import com.zt.plat.module.base.dal.dataobject.internalsupplyfactory.InternalSupplyFactoryDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;


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

/**
 * 供应链内部工厂 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class InternalSupplyFactoryServiceImpl implements InternalSupplyFactoryService {

    @Resource
    private InternalSupplyFactoryMapper internalSupplyFactoryMapper;

    @Override
    public InternalSupplyFactoryRespVO createInternalSupplyFactory(InternalSupplyFactorySaveReqVO createReqVO) {
        // 插入
        InternalSupplyFactoryDO internalSupplyFactory = BeanUtils.toBean(createReqVO, InternalSupplyFactoryDO.class);
        //校验工厂编码
        validateInternalSupplyFactoryExistsNumber(createReqVO.getNumber());
        internalSupplyFactoryMapper.insert(internalSupplyFactory);
        // 返回
        return BeanUtils.toBean(internalSupplyFactory, InternalSupplyFactoryRespVO.class);
    }

    @TenantIgnore
    public void validateInternalSupplyFactoryExistsNumber(String number) {
        if (internalSupplyFactoryMapper.exists(new LambdaQueryWrapperX<InternalSupplyFactoryDO>().eq(InternalSupplyFactoryDO::getNumber, number))) {
            throw exception(INTERNAL_SUPPLY_FACTORY_EXISTS);
        }
    }

    @Override
    public void updateInternalSupplyFactory(InternalSupplyFactorySaveReqVO updateReqVO) {
        // 校验存在
        validateInternalSupplyFactoryExists(updateReqVO.getId());
        // 更新
        InternalSupplyFactoryDO updateObj = BeanUtils.toBean(updateReqVO, InternalSupplyFactoryDO.class);
        //不允许修改除了绑定工厂名称、绑定工厂编码和操作以外的信息
        InternalSupplyFactoryDO factoryDO = new InternalSupplyFactoryDO();
        factoryDO.setId(updateObj.getId());
        factoryDO.setRelativityNumber(updateObj.getRelativityNumber());
        factoryDO.setRelativityName(updateObj.getRelativityName());
        factoryDO.setOperationType(updateObj.getOperationType());
        internalSupplyFactoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInternalSupplyFactory(Long id) {
        // 校验存在
        validateInternalSupplyFactoryExists(id);

        // 删除
        internalSupplyFactoryMapper.deleteById(id);
    }

    @Override
    public void deleteInternalSupplyFactoryListByIds(List<Long> ids) {
        // 校验存在
        validateInternalSupplyFactoryExists(ids);
        //校验能否被删除
        validateInternalSupplyFactoryCanDel(ids);
        // 删除
        internalSupplyFactoryMapper.deleteByIds(ids);
    }

    private void validateInternalSupplyFactoryCanDel(List<Long> ids) {
        List<InternalSupplyFactoryDO> list = internalSupplyFactoryMapper.selectByIds(ids);
        List<InternalSupplyFactoryDO> internalSupplyFactoryDOS =
                list.stream().filter(item -> item.getRelativityName() == null && item.getRelativityNumber() == null).toList();
        if (CollUtil.isEmpty(internalSupplyFactoryDOS) || internalSupplyFactoryDOS.size() != ids.size()) {
            throw exception(INTERNAL_SUPPLY_FACTORY_DONT_DELETE);
        }
    }

    private void validateInternalSupplyFactoryExists(List<Long> ids) {
        List<InternalSupplyFactoryDO> list = internalSupplyFactoryMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(INTERNAL_SUPPLY_FACTORY_NOT_EXISTS);
        }
    }

    private void validateInternalSupplyFactoryExists(Long id) {
        if (internalSupplyFactoryMapper.selectById(id) == null) {
            throw exception(INTERNAL_SUPPLY_FACTORY_NOT_EXISTS);
        }
    }

    @Override
    public InternalSupplyFactoryDO getInternalSupplyFactory(Long id) {
        return internalSupplyFactoryMapper.selectById(id);
    }

    @Override
    public PageResult<InternalSupplyFactoryDO> getInternalSupplyFactoryPage(InternalSupplyFactoryPageReqVO pageReqVO) {
        return internalSupplyFactoryMapper.selectPage(pageReqVO);
    }

    @Override
    public void bindFactory(List<InternalSupplyFactoryDO> bindFactoryList) {
        List<InternalSupplyFactoryDO> internalSupplyFactoryDOS = new ArrayList<>();
        for (InternalSupplyFactoryDO internalSupplyFactoryDO : bindFactoryList) {
            InternalSupplyFactoryDO internalSupplyFactory = new InternalSupplyFactoryDO();
            internalSupplyFactory.setName(internalSupplyFactoryDO.getName());
            internalSupplyFactory.setNumber(internalSupplyFactoryDO.getNumber());
            internalSupplyFactory.setType(internalSupplyFactoryDO.getType());
            internalSupplyFactory.setRelativityName(internalSupplyFactoryDO.getName());
            internalSupplyFactory.setRelativityNumber(internalSupplyFactoryDO.getNumber());
            internalSupplyFactory.setIsEnable("1");
            internalSupplyFactory.setCompanyIdCustom(internalSupplyFactoryDO.getCompanyIdCustom());
            internalSupplyFactory.setCompanyNameCustom(internalSupplyFactoryDO.getCompanyName());
            internalSupplyFactoryDOS.add(internalSupplyFactory);
        }
        internalSupplyFactoryMapper.insertBatch(internalSupplyFactoryDOS);
    }

    @Override
    public void enableInternalSupplyFactoryList(List<InternalSupplyFactoryEnableReqVO> enableReqVOS) {
        enableReqVOS.forEach(item -> {
            InternalSupplyFactoryDO internalSupplyFactory = new InternalSupplyFactoryDO();
            internalSupplyFactory.setId(Long.valueOf(item.getId()));
            internalSupplyFactory.setIsEnable(item.getIsEnable());
            internalSupplyFactoryMapper.updateById(internalSupplyFactory);
        });

    }

}