
package com.sw.mes.yb.mcs.service.impl;

import com.sw.mes.base.factory.inspect.entity.ProductAssemblyRule;
import com.sw.mes.base.factory.inspect.service.ProductAssemblyRuleService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.fls.dto.BindingDto;
import com.sw.mes.yb.mcs.dao.DislocationInfoDao;
import com.sw.mes.yb.mcs.dao.McsSquareInventoryLogDao;
import com.sw.mes.yb.mcs.dto.McsFbInventoryDto;
import com.sw.mes.yb.mcs.dto.McsSquareInventoryLogDto;
import com.sw.mes.yb.mcs.entity.McsSquareInventoryLog;
import com.sw.mes.yb.mcs.service.McsSquareInventoryLogService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JS111816
 * @Description:
 * @Date: 2023/3/9
 * @Modified By:
 **/
@Service
public class McsSquareInventoryLogServiceImpl extends BaseServiceImpl<McsSquareInventoryLog> implements McsSquareInventoryLogService {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ProductAssemblyRuleService productAssemblyRuleService;

    private McsSquareInventoryLogDao dao;
    @Autowired
    public void setDao(McsSquareInventoryLogDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Override
    public McsSquareInventoryLog queryByCrystalNo(String queryByCrystalNo) {
        return dao.queryByCrystalNo(queryByCrystalNo);
    }

    @Override
    public McsSquareInventoryLog queryByCrystalNo(String crystalNo, String inventoryType) {
        return dao.queryByCrystalNo(crystalNo, inventoryType);
    }

    @Override
    public List<McsSquareInventoryLog> queryAllByCrystalNo(String crystalNo) {
        return dao.queryAllByCrystalNo(crystalNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByCrystalNoList(List<McsFbInventoryDto> fbInventoryDto) {
        if (CollectionUtils.isEmpty(fbInventoryDto)) {
            throw new BusinessException("单晶编号集合不能为空");
        }

        for (int i = 0; i < fbInventoryDto.size(); i++) {
            McsFbInventoryDto mcsFbInventoryDto = fbInventoryDto.get(i);
            if (StringUtils.isEmpty(mcsFbInventoryDto.getCrystalNo())){
                throw new BusinessException("单晶编号不能为空");
            }
            dao.deleteByCrystalNoName(mcsFbInventoryDto.getCrystalNo());

        }
        return fbInventoryDto.size();
    }

    @Override
    public McsSquareInventoryLogDto queryMcsSquareLog(BindingDto bindingDto) {
        McsSquareInventoryLog mcsSquareInventoryLog = dao.queryByCrystalNo(bindingDto.getSectionNo());
        if(null == mcsSquareInventoryLog){
            throw new RuntimeException("未查询到入库记录");
        }
        McsSquareInventoryLogDto mcsSquareInventoryLogDto = toLazyDto(mcsSquareInventoryLog);
        String packBox = mcsSquareInventoryLog.getPackBox();
        ProductAssemblyRule ruleByStation = productAssemblyRuleService.findRuleByIssuePpoint(packBox);
        if(null != ruleByStation && !StringUtils.isBlank(ruleByStation.getStation())){
            mcsSquareInventoryLogDto.setName(ruleByStation.getStation());

            return mcsSquareInventoryLogDto;
        }
        return null;
    }

    public McsSquareInventoryLogDto toLazyDto(McsSquareInventoryLog entity) {
        return modelMapper.map(entity, McsSquareInventoryLogDto.class);
    }

    @Override
    public int deleteByCrystalNo(String sectionNo) {
        return dao.deleteByCrystalNo(sectionNo);
    }

    @Override
    public int deleteByCrystalNoAndInventoryType(String sectionNo, String inventoryType) {
        return dao.deleteByCrystalNoAndInventoryType(sectionNo,inventoryType);
    }

    @Override
    public List<McsSquareInventoryLog> queryByGroupId(String groupId) {
        return dao.queryByGroupId(groupId);
    }

    @Override
    public void updateInventoryType(String sectionNo, String newInventoryType,String oldInventoryType) {
        dao.updateInventoryType(sectionNo,newInventoryType,oldInventoryType);
    }
}
