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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreverwin.mesnac.common.constant.Constants;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.model.Tool;
import com.foreverwin.mesnac.common.service.CommonService;
import com.foreverwin.mesnac.common.service.ToolService;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.dto.SfcDto;
import com.foreverwin.mesnac.production.mapper.CutterLogMapper;
import com.foreverwin.mesnac.production.mapper.SfcCrossMapper;
import com.foreverwin.mesnac.production.model.CutterLog;
import com.foreverwin.mesnac.production.service.CutterLogService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 刀具记录表 服务实现类
 * </p>
 *
 * @author Philip
 * @since 2021-07-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CutterLogServiceImpl extends ServiceImpl<CutterLogMapper, CutterLog> implements CutterLogService {


    @Autowired
    private CutterLogMapper cutterLogMapper;
    @Autowired
    private SfcCrossMapper sfcCorssMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ToolService toolService;
    @Autowired
    private CutterLogService cutterLogService;

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

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

    @Override
    public void doAdd(Map<String, Object> map) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        String sfc = (String)map.get("sfc");
        String resrce = (String)map.get("resrce");
        List<CutterLog> list = (List<CutterLog>)map.get("cutterList");
        ObjectMapper mapper = new ObjectMapper();
        list = mapper.convertValue(list, new TypeReference<List<CutterLog>>() {});
        SfcDto sfcDto = new SfcDto();
        sfcDto.setSfc(sfc);
        sfcDto.setResrce(resrce);
        Map<String, Object> sfcData = sfcCorssMapper.querySfcData(site, LocaleContextHolder.getLocale().getLanguage(), sfcDto);
        if (sfcData == null) {
            throw new BaseException("根据当前资源未找到条码[" + sfc + "]的基本信息！");
        }
        String operation = (String)sfcData.get("OPERATION");
        String stepId = (String)sfcData.get("STEP_ID");
        String shopOrder = (String)sfcData.get("SHOP_ORDER");
        String item = (String)sfcData.get("ITEM");
        String workShopBo = commonService.getWorkShopBo(HandleEnum.RESOURCE.getHandle(site, resrce));
        if (StringUtil.isBlank(workShopBo)) {
            throw new BaseException("资源 " + resrce + " 未匹配到车间");
        }
        String workShop = StringUtil.trimHandle(workShopBo);
        if(list==null||list.size()<1){
            throw new BaseException("刀具列表不能为空");
        }
        list.forEach(cutterLog->{
            String handle = UUID.randomUUID().toString();
            cutterLog.setHandle(handle);
            //校验刀具是否存在
            String cutter = cutterLog.getCutter();
            QueryWrapper<Tool> query=new QueryWrapper<>();
            query.eq(Tool.SITE,site);
            query.eq(Tool.TOOL,cutter);
            query.eq(Tool.STATUS, Constants.STATUS_Y);
            List<Tool> toolList = toolService.list(query);
            if (toolList.size()<1){
                throw new BaseException("不存在的刀具");
            }
            cutterLog.setSite(site);
            cutterLog.setShopOrder(shopOrder);
            cutterLog.setWorkCenter(workShop);
            cutterLog.setSfc(sfc);
            cutterLog.setOperation(operation);
            cutterLog.setStepId(stepId);
            cutterLog.setItem(item);
            cutterLog.setResrce(resrce);
            cutterLog.setCreateUser(user);
            cutterLog.setCreatedDateTime(LocalDateTime.now());
            save(cutterLog);
        });

    }

    @Override
    public List<CutterLog> findToolInfoBySfc(CutterLog cutterLog) {
        String site = CommonMethods.getSite();
        cutterLog.setSite(site);
        List<CutterLog> cutterLogList = cutterLogService.selectList(cutterLog);
        if (cutterLogList != null && cutterLogList.size() > 0){
            for (CutterLog cutter:cutterLogList) {
                Tool byId = toolService.getById(HandleEnum.TOOL.getHandle(site, cutter.getCutter()));
                cutter.setDescription(byId.getDescription());
                cutter.setSpecification(byId.getSpecification());
            }
            return cutterLogList;
        }else {
            return null;
        }
    }


}