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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.model.InspectionItem;
import com.foreverwin.mesnac.common.service.InspectionItemDetailService;
import com.foreverwin.mesnac.common.service.InspectionItemService;
import com.foreverwin.mesnac.equip.model.ResourceInspectResource;
import com.foreverwin.mesnac.equip.service.ResourceInspectResourceService;
import com.foreverwin.modular.core.util.CommonMethods;
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.equip.model.ResourceInspectPlan;
import com.foreverwin.mesnac.equip.mapper.ResourceInspectPlanMapper;
import com.foreverwin.mesnac.equip.service.ResourceInspectPlanService;
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.List;
/**
 * <p>
 * 设备检验计划表 服务实现类
 * </p>
 *
 * @author pavel.Liu
 * @since 2021-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceInspectPlanServiceImpl extends ServiceImpl<ResourceInspectPlanMapper, ResourceInspectPlan> implements ResourceInspectPlanService {


    @Autowired
    private ResourceInspectPlanMapper resourceInspectPlanMapper;

    @Autowired
    public  ResourceInspectPlanService resourceInspectPlanService;

    @Autowired
    public InspectionItemService inspectionItemService;
    @Autowired
    public InspectionItemDetailService inspectionItemDetailService;

    @Autowired
    public ResourceInspectResourceService resourceInspectResourceService;



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

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

    @Override
    public boolean saveResourcePlanData(ResourceInspectPlan resourceInspectPlan) {
        // 创建人员
        String user = CommonMethods.getUser();

        //查询设备计划是否存在
        String site = CommonMethods.getSite();
        String planNo = resourceInspectPlan.getPlanNo();
        String revision =  resourceInspectPlan.getRevision();
        String handle = HandleEnum.RESOURCE_INSPECT_PLAN.getHandle(site, planNo, revision);
        ResourceInspectPlan resourceInspectPlanByBanlde = resourceInspectPlanService.getById(handle);
        //计划是否已存在： 更新计划主表
            //1、计划不存在： 新增计划主表
        if (resourceInspectPlanByBanlde==null){

        }
            //2、计划存在： 更新计划主表

        //保存计划下设备清单(全删全增)
            // 1、全删
        ResourceInspectResource resourceInspect = new ResourceInspectResource();
        resourceInspect.setResourceInspectPlanBo(handle);
        QueryWrapper<ResourceInspectResource> resourceWrapper = new QueryWrapper<>();
        resourceWrapper.setEntity(resourceInspect);
//        resourceInspectResourceService.remove(resourceWrapper);
            // 2、全增
        List<ResourceInspectResource> equips = resourceInspectPlan.getMaintainPlanEquipList();
        for (ResourceInspectResource equip: equips) {
            equip.setResourceInspectPlanBo(handle);
            equip.setCreateUser(user);
            equip.setCreatedDateTime(LocalDateTime.now());
//            resourceInspectResourceService.save(equip);
        }

        // 判断检验项目清单HANDLE是否存在
        String inspectionItemBo = resourceInspectPlan.getInspectionItemBo();
        InspectionItem inspectionItem = new InspectionItem();
        inspectionItem.setHandle(inspectionItemBo);
        QueryWrapper<InspectionItem> inspectionItemWrapper = new QueryWrapper<>();
        inspectionItemWrapper.setEntity(inspectionItem);
        List<InspectionItem> inspectionItems = inspectionItemService.list(inspectionItemWrapper);
            // 如果不存在，则返回错误
        if (inspectionItems==null||inspectionItems.size()<1){
            return false;
        }
        resourceInspectPlanService.saveOrUpdate(resourceInspectPlan);
        return false;
    }


}