package com.foreverwin.mesnac.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.foreverwin.mesnac.common.constant.CustomFieldConstant;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.meapi.model.Sfc;
import com.foreverwin.mesnac.meapi.model.WorkCenter;
import com.foreverwin.mesnac.meapi.service.SfcService;
import com.foreverwin.mesnac.meapi.service.WorkCenterService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.production.model.SfcData;
import com.foreverwin.mesnac.production.mapper.SfcDataMapper;
import com.foreverwin.mesnac.production.service.SfcDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Leon.L
 * @since 2021-06-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SfcDataServiceImpl extends ServiceImpl<SfcDataMapper, SfcData> implements SfcDataService {

    @Autowired
    private SfcService sfcService;
    @Autowired
    private SfcDataMapper sfcDataMapper;
    @Autowired
    private WorkCenterService workCenterService;

    @Override
    public IPage<SfcData> selectPage(FrontPage<SfcData> frontPage, SfcData sfcData) {
        QueryWrapper<SfcData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcData);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<SfcData> selectList(SfcData sfcData) {
        QueryWrapper<SfcData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcData);
        return super.list(queryWrapper);
    }

    @Override
    public void saveSfcLocation(String site, String user, String sfc, String location) {
        String sfcBo = HandleEnum.SFC.getHandle(site, sfc);
        Sfc sfcModel = sfcService.getById(sfcBo);
        if (sfcModel == null) {
            throw BusinessException.build("SFC【"  +sfc+"】不存在");
        }

        //位置所录入的字符串“_”前的字符为车间代码
        if (!location.contains("_")) {
            throw BusinessException.build("输入的位置【" +location+ "】格式不正确：必须包含符号【_】,并且符号之前必须是车间代码");
        }
        String workCenterBo = HandleEnum.WORK_CENTER.getHandle(site, location.split("_")[0]);
        WorkCenter workCenterModel = workCenterService.getById(workCenterBo);
        if (workCenterModel == null || !"LEVEL4".equals(workCenterModel.getWcCategory())) {
            throw BusinessException.build("位置【"  +location+"】中符号【_】之前的不是车间代码");
        }

        LocalDateTime nowDate = LocalDateTime.now();
        //查询是否存在
        Map<String, Object> map = new HashMap<>();
        map.put(SfcData.SFC_BO, sfcBo);
        map.put(SfcData.DATA_FIELD, CustomFieldConstant.SFC_DATA_LOCATION);
        List<SfcData> sfcDataList = sfcDataMapper.selectByMap(map);

        if (sfcDataList == null || sfcDataList.size() <= 0) {
            //记录位置到SFC
            SfcData sfcData = new SfcData();
            sfcData.setHandle(HandleEnum.SFC_DATA.getHandle(site, sfcBo, CustomFieldConstant.SFC_DATA_LOCATION));
            sfcData.setSfcBo(sfcBo);
            sfcData.setDataField(CustomFieldConstant.SFC_DATA_LOCATION);
            sfcData.setDataAttr(location);
            sfcData.setDateTime(nowDate);
            sfcData.setUserBo(HandleEnum.USER.getHandle(site, user));
            sfcData.setCreatedDateTime(nowDate);
            sfcDataMapper.insert(sfcData);
        } else {
            //更新位置到SFC
            SfcData sfcData = sfcDataList.get(0);
            sfcData.setSfcBo(sfcBo);
            sfcData.setDataField(CustomFieldConstant.SFC_DATA_LOCATION);
            sfcData.setDataAttr(location);
            sfcData.setModifiedDateTime(nowDate);
            sfcDataMapper.updateDataAttr(sfcData);
        }
    }


}