package com.aucma.production.service.impl;

import java.util.List;

import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.production.domain.ProdStationCapability;
import com.aucma.production.mapper.ProdStationCapabilityMapper;
import com.aucma.production.service.IProdStationCapabilityService;

@Service
public class ProdStationCapabilityServiceImpl implements IProdStationCapabilityService {
    @Autowired
    private ProdStationCapabilityMapper prodStationCapabilityMapper;

    @Override
    public ProdStationCapability selectProdStationCapabilityByObjId(Long objId) {
        return prodStationCapabilityMapper.selectProdStationCapabilityByObjId(objId);
    }

    @Override
    public List<ProdStationCapability> selectProdStationCapabilityList(ProdStationCapability prodStationCapability) {
        return prodStationCapabilityMapper.selectProdStationCapabilityList(prodStationCapability);
    }

    @Override
    public int insertProdStationCapability(ProdStationCapability prodStationCapability) {
        validateCapability(prodStationCapability);
        // 为什么按“工位+物料”做唯一控制：这两个维度共同定义一条能力口径，重复落库会让排产和产能维护出现冲突。
        ensureUnique(prodStationCapability);
        prodStationCapability.setCreatedTime(DateUtils.getNowDate());
        return prodStationCapabilityMapper.insertProdStationCapability(prodStationCapability);
    }

    @Override
    public int updateProdStationCapability(ProdStationCapability prodStationCapability) {
        validateCapability(prodStationCapability);
        ensureUnique(prodStationCapability);
        prodStationCapability.setUpdatedTime(DateUtils.getNowDate());
        return prodStationCapabilityMapper.updateProdStationCapability(prodStationCapability);
    }

    @Override
    public int deleteProdStationCapabilityByObjIds(Long[] objIds) {
        return prodStationCapabilityMapper.deleteProdStationCapabilityByObjIds(objIds);
    }

    private void validateCapability(ProdStationCapability prodStationCapability) {
        if (prodStationCapability == null) {
            throw new ServiceException("工位能力数据不能为空");
        }
        if (StringUtils.isBlank(prodStationCapability.getStationCode())) {
            throw new ServiceException("工位不能为空");
        }
        if (StringUtils.isBlank(prodStationCapability.getMaterialCode())) {
            throw new ServiceException("物料不能为空");
        }
        if (StringUtils.isBlank(prodStationCapability.getStatus())) {
            prodStationCapability.setStatus("0");
        }
    }

    private void ensureUnique(ProdStationCapability prodStationCapability) {
        ProdStationCapability existed = prodStationCapabilityMapper.selectByStationCodeAndMaterialCode(prodStationCapability);
        if (existed != null && !existed.getObjId().equals(prodStationCapability.getObjId())) {
            throw new ServiceException("同一工位下该物料的能力维护已存在");
        }
    }
}
