package cn.iocoder.yudao.module.mes.service.moldoperate;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
import cn.iocoder.yudao.module.common.dal.mysql.mold.MoldMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerMapper;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.mes.controller.admin.moldoperate.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldoperate.MoldOperateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mes.dal.mysql.moldoperate.MoldOperateMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;

/**
 * 模具上下模 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class MoldOperateServiceImpl implements MoldOperateService {

    @Resource
    private MoldOperateMapper moldOperateMapper;

    @Resource
    private DeviceLedgerMapper deviceLedgerMapper;

    @Resource
    private MoldMapper moldMapper;

    @Override
    public Long createMoldOperate(MoldOperateSaveReqVO createReqVO) {
        // 插入
        MoldOperateDO moldOperate = BeanUtils.toBean(createReqVO, MoldOperateDO.class);
        DeviceLedgerDO deviceLedgerDO = deviceLedgerMapper.selectById(createReqVO.getDeviceId());
        moldOperate.setDeviceName(deviceLedgerDO.getDeviceName());

        List<Long> idList;

        if (createReqVO.getLowerMoldId()!=null){
            idList = Arrays.stream(createReqVO.getLowerMoldId().split(","))
                    .map(String::trim) // 去除可能存在的空格
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
        }else {
            idList = Arrays.stream(createReqVO.getMoldId().split(","))
                    .map(String::trim) // 去除可能存在的空格
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
        }



        if (!idList.isEmpty()) {
            StringBuilder moldNameBuilder = new StringBuilder();
            for (Long id : idList) {
                MoldDO moldDO = moldMapper.selectById(id);
                if (moldDO != null && StringUtil.isNotBlank(moldDO.getName())) {
                    if (moldNameBuilder.length() > 0) {
                        moldNameBuilder.append(",");
                    }
                    moldNameBuilder.append(moldDO.getName());
                }
            }
            moldOperate.setMoldName(moldNameBuilder.toString());
        } else {
            moldOperate.setMoldName("");
        }

        moldOperateMapper.insert(moldOperate);
        // 返回
        return moldOperate.getId();
    }

    @Override
    public void updateMoldOperate(MoldOperateSaveReqVO updateReqVO) {
        // 校验存在
        validateMoldOperateExists(updateReqVO.getId());

        // 更新
        MoldOperateDO updateObj = BeanUtils.toBean(updateReqVO, MoldOperateDO.class);

        DeviceLedgerDO deviceLedgerDO = deviceLedgerMapper.selectById(updateReqVO.getDeviceId());
        updateObj.setDeviceName(deviceLedgerDO.getDeviceName());

        List<Long> idList = Arrays.stream(updateReqVO.getMoldId().split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());

        if (!idList.isEmpty()) {
            StringBuilder moldNameBuilder = new StringBuilder();
            for (Long id : idList) {
                MoldDO moldDO = moldMapper.selectById(id);
                if (moldDO != null && StringUtil.isNotBlank(moldDO.getName())) {
                    if (moldNameBuilder.length() > 0) {
                        moldNameBuilder.append(",");
                    }
                    moldNameBuilder.append(moldDO.getName());
                }
            }
            updateObj.setMoldName(moldNameBuilder.toString());
        } else {
            updateObj.setMoldName("");
        }

        moldOperateMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoldOperate(Long id) {
        // 校验存在
        validateMoldOperateExists(id);
        // 删除
        moldOperateMapper.deleteById(id);
    }

    private void validateMoldOperateExists(Long id) {
        if (moldOperateMapper.selectById(id) == null) {
            throw exception(MOLD_OPERATE_NOT_EXISTS);
        }
    }

    @Override
    public MoldOperateDO getMoldOperate(Long id) {
        return moldOperateMapper.selectById(id);
    }

    @Override
    public PageResult<MoldOperateDO> getMoldOperatePage(MoldOperatePageReqVO pageReqVO) {
        return moldOperateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MoldDO> getLowerMoldList(Long id) {

        List<MoldDO> moldDOList = new ArrayList<>();

        DeviceLedgerDO deviceLedgerDO = deviceLedgerMapper.selectById(id);
        if (StringUtil.isBlank(deviceLedgerDO.getMoldId())){
            return new ArrayList<>();
        }
        List<Long> idList = Arrays.stream(deviceLedgerDO.getMoldId().split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());

        for (Long moldId : idList) {
            MoldDO moldDO = moldMapper.selectById(moldId);
            if (moldDO != null){
                moldDOList.add(moldDO);
            }
        }


        return moldDOList;
    }


}