package com.sw.mes.fb.instoreapply.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.fb.instoreapply.dao.InstoreApplyDao;
import com.sw.mes.fb.instoreapply.dao.InstoreApplyItemDao;
import com.sw.mes.fb.instoreapply.entity.InstoreApply;
import com.sw.mes.fb.instoreapply.entity.InstoreApplyItem;
import com.sw.mes.fb.instoreapply.service.InstoreApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class InstoreApplyServiceImpl extends BaseServiceImpl<InstoreApply> implements InstoreApplyService {
    InstoreApplyDao dao;
	@Autowired
    public void setDao(InstoreApplyDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    InstoreApplyItemDao itemDao;

    //检查list是否有重复 判断是否填写过入库申请单
    public void checkSectionNosIn(List<InstoreApplyItem> items , String productLevelName){
        if(items==null||items.size()==0){
            throw new RuntimeException("晶段明细不能为空");
        }
        for(InstoreApplyItem instoreApplyItem:items){
            if(StringUtils.isBlank(instoreApplyItem.getSectionNoFb())){
                throw new RuntimeException("错误，晶段编号不能为空");
            }
        }
        //判断产品等级是否和选择的一致
//        for(InstoreApplyItem instoreApplyItem:items){
//            if(instoreApplyItem.getProductLevelName()==null||!instoreApplyItem.getProductLevelName().equals(productLevelName)){
//                throw new RuntimeException(instoreApplyItem.getSectionNoFb()+"产品等级不一致");
//            }
//        }
        //判断是否填写过入库申请单
        List<String> sectionItems = new ArrayList<>();
        List<String> checkSectionItems = new ArrayList<>();
        for(InstoreApplyItem instoreApplyItem:items){
            if(instoreApplyItem.getId()==null||instoreApplyItem.getId()==0){
                if(instoreApplyItem.getSectionNoFb()!=null){
                    sectionItems.add(instoreApplyItem.getSectionNoFb());
                    if(instoreApplyItem.getCheckoutName()!=null&&instoreApplyItem.getCheckoutName().equals("一次检验")){
                        checkSectionItems.add(instoreApplyItem.getSectionNoFb());
                    }
                }
            }
        }
        //List<String> sectionItems=items.stream().map(InstoreApplyItem::getSectionNo).collect(Collectors.toList());
        //判断是否传入的list是否有重复晶段编号
        if(sectionItems.size()!=new HashSet<String>(sectionItems).size()) {
            throw new RuntimeException("晶段编号不能重复");
        }
        if(checkSectionItems!=null&&checkSectionItems.size()>0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(now);
            ca.add(Calendar.DATE,-300);
            String nowStr = sdf.format(ca.getTime());
            List<InstoreApplyItem> checkList = itemDao.getItemsBySectionNos(checkSectionItems,nowStr);
            if(checkList!=null&&checkList.size()>0){
                List<String> inInfo =checkList.stream().map(InstoreApplyItem::getSectionNoFb).collect(Collectors.toList());
                throw new RuntimeException(String.join(",",inInfo)+"等单晶已填写过入库申请");
            }
        }

    }
    //更新入库申请时 卡控一次检验
    //检查list是否有重复 判断是否填写过入库申请单
    public void checkSectionNosUpdate(List<InstoreApplyItem> items , String productLevelName, Integer applyId){
        if(items==null||items.size()==0){
            throw new RuntimeException("晶段明细不能为空");
        }
        for(InstoreApplyItem instoreApplyItem:items){
            if(StringUtils.isBlank(instoreApplyItem.getSectionNoFb())){
                throw new RuntimeException("错误，晶段编号不能为空");
            }
        }
        //判断产品等级是否和选择的一致
//        for(InstoreApplyItem instoreApplyItem:items){
//            if(instoreApplyItem.getProductLevelName()==null||!instoreApplyItem.getProductLevelName().equals(productLevelName)){
//                throw new RuntimeException(instoreApplyItem.getSectionNoFb()+"产品等级不一致");
//            }
//        }
        //判断是否填写过入库申请单
        List<String> sectionItems = new ArrayList<>();
        List<String> checkSectionItems = new ArrayList<>();
        for(InstoreApplyItem instoreApplyItem:items){
            if(instoreApplyItem.getSectionNoFb()!=null){
                sectionItems.add(instoreApplyItem.getSectionNoFb());
                if(instoreApplyItem.getCheckoutName()!=null&&instoreApplyItem.getCheckoutName().equals("一次检验")){
                    checkSectionItems.add(instoreApplyItem.getSectionNoFb());
                }
            }
        }
        //List<String> sectionItems=items.stream().map(InstoreApplyItem::getSectionNo).collect(Collectors.toList());
        //判断是否传入的list是否有重复晶段编号
        if(sectionItems.size()!=new HashSet<String>(sectionItems).size()) {
            throw new RuntimeException("晶段编号不能重复");
        }
        if(checkSectionItems!=null&&checkSectionItems.size()>0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(now);
            ca.add(Calendar.DATE,-300);
            String nowStr = sdf.format(ca.getTime());
            List<InstoreApplyItem> checkList = itemDao.getItemsBySectionNosAndTrayId(checkSectionItems,nowStr,applyId);
            if(checkList!=null&&checkList.size()>0){
                List<String> inInfo =checkList.stream().map(InstoreApplyItem::getSectionNoFb).collect(Collectors.toList());
                throw new RuntimeException(String.join(",",inInfo)+"等单晶已填写过入库申请");
            }
        }
    }

    @Override
    public void createIn(InstoreApply instoreApply, List<InstoreApplyItem> items){
	    //判断是否已填写入库申请
        checkSectionNosIn(items,instoreApply.getProductLevelName());
        Date now = new Date();
        //时间
        instoreApply.setTime(now);
        Calendar ca = Calendar.getInstance();
        ca.setTime(now);
        ca.add(Calendar.MINUTE,-510);
        //日期 减去8：30  510分钟
        instoreApply.setDate(ca.getTime());
        instoreApply.setCount(items.size());
        //生成单号
        //8:30-8:30查询当天最大顺序号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDateStr=sdf.format(ca.getTime());
        Integer lastSort = dao.getMaxSortByDate(nowDateStr);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        String orderNoMMdd = sdf1.format(ca.getTime());
        String sortStr = String.format("%03d", lastSort+1);
        String orderNo = orderNoMMdd+sortStr;
        instoreApply.setOrderNo(orderNo);
        instoreApply.setState("未打印");
        instoreApply.setSort(lastSort+1);
        dao.save(instoreApply);
        //明细表set冗余字段
        for(InstoreApplyItem instoreApplyItem:items){

            instoreApplyItem.setId(null);
            instoreApplyItem.setApplyId(instoreApply.getId());
            instoreApplyItem.setOrderNo(instoreApply.getOrderNo());
            instoreApplyItem.setTime(now);
            instoreApplyItem.setRemark(instoreApply.getRemark());
            instoreApplyItem.setUser(instoreApply.getUser());
            instoreApplyItem.setState("未打印");
            instoreApplyItem.setHasGone(instoreApply.getHasGone());
        }
        itemDao.saveAll(items);
    }

    @Override
    public void updateIn(InstoreApply instoreApply, List<InstoreApplyItem> items){
        //判断是否已填写入库申请
        checkSectionNosUpdate(items,instoreApply.getProductLevelName(),instoreApply.getId());
        Date now = new Date();
        //时间
        instoreApply.setTime(now);
        instoreApply.setCount(items.size());
        dao.save(instoreApply);
        //删除明细
        itemDao.deleteByApplyId(instoreApply.getId());
        //明细表set冗余字段
        for(InstoreApplyItem instoreApplyItem:items){
            instoreApplyItem.setId(null);
            instoreApplyItem.setApplyId(instoreApply.getId());
            instoreApplyItem.setOrderNo(instoreApply.getOrderNo());
            instoreApplyItem.setTime(now);
            instoreApplyItem.setRemark(instoreApply.getRemark());
            instoreApplyItem.setUser(instoreApply.getUser());
            instoreApplyItem.setHasGone(instoreApply.getHasGone());
        }
        itemDao.saveAll(items);
    }

    @Override
    public void deleteIn(Integer id){
        InstoreApply entity = dao.getOne(id);
        if(entity==null){
            throw new RuntimeException("此入库申请单不存在");
        }
        itemDao.deleteByApplyId(id);
        dao.delete(entity);
    }

    @Override
    public void print(Integer id){
        InstoreApply entity = dao.getOne(id);
        entity.setState("已打印");
        dao.save(entity);
        itemDao.updateStateById(id);
    }

    @Override
    public List<InstoreApply> findByOrderNo(String orderNo) {
        return dao.findByOrderNo(orderNo);
    }
}
