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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.constant.Constants;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.mapper.InspectionItemMapper;
import com.foreverwin.mesnac.common.model.InspectionItem;
import com.foreverwin.mesnac.common.model.InspectionItemAddition;
import com.foreverwin.mesnac.common.model.InspectionItemDetail;
import com.foreverwin.mesnac.common.service.InspectionItemAdditionService;
import com.foreverwin.mesnac.common.service.InspectionItemDetailService;
import com.foreverwin.mesnac.common.service.InspectionItemService;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.visiprise.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 检验项目维护主表 服务实现类
 * </p>
 *
 * @author Philip
 * @since 2021-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InspectionItemServiceImpl extends ServiceImpl<InspectionItemMapper, InspectionItem> implements InspectionItemService {


    @Autowired
    private InspectionItemMapper inspectionItemMapper;
    @Autowired
    private InspectionItemDetailService inspectionItemDetailService;
    @Autowired
    private InspectionItemAdditionService inspectionItemAdditionService;

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

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

    @Override
    public String findRevisionByCurrentRevision(String inspectionItemNo) {
        String site = CommonMethods.getSite();
        LambdaQueryWrapper<InspectionItem> lambdaQuery=new LambdaQueryWrapper<>();
        lambdaQuery.eq(InspectionItem::getInspectionItemNo,inspectionItemNo);
        lambdaQuery.eq(InspectionItem::getCurrentRevision, Constants.BOOL_TRUE);
        lambdaQuery.eq(InspectionItem::getSite, site);
        InspectionItem one = getOne(lambdaQuery, false);
        if (one!=null){
            return one.getRevision();
        }
        return null;
    }

    @Override
    public String inspectItemNoGenerationRules(InspectionItem inspectionItem) {
        List<InspectionItemAddition> inspectionItemAdditionList = inspectionItem.getInspectionItemAdditionList();
        StringBuilder inspectionItemNo=new StringBuilder();
        inspectionItemAdditionList.forEach(inspectionItemAddition->inspectionItemNo.append(inspectionItemAddition.getAdditionalObject()));
        return inspectionItemNo.toString();
    }

    @Override
    public void saveAll(InspectionItem inspectionItem) {
        boolean b = false;
        if(StringUtil.isEmpty(inspectionItem.getInspectionItemNo())){
            b = true;
            inspectionItem.setInspectionItemNo(inspectItemNoGenerationRules(inspectionItem));
        }
        if (inspectionItem.getInspectionType().length()>1){
            inspectionItem.setCategory("EQ");
        }else {
            inspectionItem.setCategory("QC");
        }
        inspectionItem.setSite(CommonMethods.getSite());
        if (StringUtil.isBlank(inspectionItem.getRevision())){
            inspectionItem.setRevision("1");
        }
        inspectionItem.setHandle(HandleEnum
                .INSPECTION_ITEM.getHandle(inspectionItem.getSite(),inspectionItem.getInspectionItemNo(),inspectionItem.getRevision()));
        //保存检验项目维护-副
        //移除
        HashMap<String, Object> removeCondition = new HashMap<>();
        removeCondition.put(InspectionItemDetail.INSPECTION_ITEM_BO,inspectionItem.getHandle());
        inspectionItemDetailService.removeByMap(removeCondition);
        inspectionItemAdditionService.removeByMap(removeCondition);

        if(null != inspectionItem.getInspectionItemDetailList() && inspectionItem.getInspectionItemDetailList().size() > 0){
            List<InspectionItemDetail> inspectionItemDetailList = inspectionItem.getInspectionItemDetailList();
            for (InspectionItemDetail inspectionItemDetail : inspectionItemDetailList) {
                inspectionItemDetail.setHandle(UUID.randomUUID().toString());
                inspectionItemDetail.setInspectionItemBo(inspectionItem.getHandle());
                inspectionItemDetail.setCreatedDateTime(LocalDateTime.now());
                inspectionItemDetail.setCreateUser(CommonMethods.getUser());
                inspectionItemDetail.setModifiedDateTime(LocalDateTime.now());
                inspectionItemDetail.setModifyUser(CommonMethods.getUser());
            }
            //保存
            inspectionItemDetailService.saveBatch(inspectionItemDetailList);
        }
        if(null != inspectionItem.getInspectionItemAdditionList() && inspectionItem.getInspectionItemAdditionList().size() > 0){
            List<InspectionItemAddition> inspectionItemAdditionList = inspectionItem.getInspectionItemAdditionList();
            for (InspectionItemAddition inspectionItemAddition : inspectionItemAdditionList) {
                inspectionItemAddition.setHandle(UUID.randomUUID().toString());
                inspectionItemAddition.setInspectionItemBo(inspectionItem.getHandle());
                inspectionItemAddition.setCreatedDateTime(LocalDateTime.now());
                inspectionItemAddition.setCreateUser(CommonMethods.getUser());
                inspectionItemAddition.setModifiedDateTime(LocalDateTime.now());
                inspectionItemAddition.setModifyUser(CommonMethods.getUser());
            }
            //保存
            inspectionItemAdditionService.saveBatch(inspectionItemAdditionList);
        }

        //保存检验项目维护-主
        if (inspectionItem.getCurrentRevision().equals(Constants.BOOL_TRUE)){
            String inspectionItemNo = inspectionItem.getInspectionItemNo();
            UpdateWrapper<InspectionItem> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq(InspectionItem.INSPECTION_ITEM_NO,inspectionItemNo);
            updateWrapper.eq(InspectionItem.SITE,inspectionItem.getSite());
            updateWrapper.ne(InspectionItem.REVISION,inspectionItem.getRevision());
            updateWrapper.set(InspectionItem.CURRENT_REVISION,Constants.BOOL_FALSE);
            update(updateWrapper);
        }
        if(inspectionItem.selectById(inspectionItem.getHandle()) == null){
            inspectionItem.setCreatedDateTime(LocalDateTime.now());
            inspectionItem.setCreateUser(CommonMethods.getUser());
            inspectionItem.setModifiedDateTime(LocalDateTime.now());
            inspectionItem.setModifyUser(CommonMethods.getUser());
            save(inspectionItem);
        }else{
            inspectionItem.setModifiedDateTime(LocalDateTime.now());
            inspectionItem.setModifyUser(CommonMethods.getUser());
            if(b){
                throw new BaseException("请先检索再保存");
            }
            saveOrUpdate(inspectionItem);
        }
    }

    @Override
    public void deleteServece(InspectionItem inspectionItem) {
        if(StringUtil.isBlank(inspectionItem.getInspectionItemNo())){
            throw new BaseException("请输入编号");
        }
        if(StringUtil.isBlank(inspectionItem.getRevision())){
            throw new BaseException("请输入版本");
        }
        inspectionItem.setSite(CommonMethods.getSite());
        inspectionItem.setHandle(HandleEnum.INSPECTION_ITEM.getHandle(inspectionItem.getSite(),inspectionItem.getInspectionItemNo(),inspectionItem.getRevision()));
        HashMap<String, Object> removeCondition = new HashMap<>();
        removeCondition.put(InspectionItemAddition.INSPECTION_ITEM_BO,inspectionItem.getHandle());
        inspectionItemAdditionService.removeByMap(removeCondition);
        inspectionItemDetailService.removeByMap(removeCondition);
        removeById(inspectionItem.getHandle());
    }


}