package com.cf.imes.module.manage.service.patching;


import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.manage.controller.admin.pack.vo.PlateDetailsRespVO;
import com.cf.imes.module.manage.controller.admin.patching.vo.*;
import com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.OrderPlateRespVO;
import com.cf.imes.module.manage.dal.dataobject.patching.PatchDetailDO;
import com.cf.imes.module.manage.dal.dataobject.plate.OrderPlateDO;
import com.cf.imes.module.manage.dal.mysql.patching.MachineMapper;
import com.cf.imes.module.manage.dal.mysql.patching.PatchDetailMapper;
import com.cf.imes.module.manage.dal.mysql.plate.OrderPlateMapper;
import com.cf.imes.module.manage.dal.mysql.plate.PlateMapper;
import com.cf.imes.module.manage.dal.mysql.process.ProductionProcessMapper;
import com.cf.imes.module.manage.util.page.PageHelperUtil;
import com.cf.imes.module.manage.dal.dataobject.patching.MachineDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

@Service
@Validated
public class PatchingPlateServiceImpl implements PatchingPlateService{


    @Resource
    private ProductionProcessMapper productionProcessMapper;

    @Resource
    private OrderPlateMapper orderPlateMapper;

    @Resource
    private MachineMapper machineMapper;

    @Resource
    private PatchDetailMapper patchDetailMapper;



    // 批量加入补板查询
    @Override
    public PageResult<PatchingPlateRespVO> patchingSearchPlate(PatchingPlatePageReqVO pageReqVO) {


        // 查询板件和订单的基础信息
        PageHelper.startPage(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<PatchingPlateRespVO> patchingPlateRespVOS = productionProcessMapper.selectPatchingPlate(pageReqVO);

        //
        PageInfo<PatchingPlateRespVO> pageInfo = new PageInfo<>(patchingPlateRespVOS);
        return PageHelperUtil.convertToPageResult(pageInfo);


    }




    // 查询详细的板件
    @Override
    public PageResult<PatchingDetailRespVO> searchPlateDetail(PatchingPlatePageReqVO pageReqVO) {

        // 查询每个订单对应的板件的详细信息
        PageHelper.startPage(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<PatchingDetailRespVO> plateDetailsRespVOS = orderPlateMapper.selectPlateDetail(pageReqVO);

        //
        PageInfo<PatchingDetailRespVO> pageInfo = new PageInfo<>(plateDetailsRespVOS);
        return PageHelperUtil.convertToPageResult(pageInfo);


    }






    // 加入的板件信息
    @Override
    public PatchingDetailRespVO searchPatchingList(Long plateId) {


        return orderPlateMapper.selectPlate(plateId);


    }







    // 优化排版
    @Override
    public List<PatchingDetailRespVO> optimizeLayout(PatchingDetailReqVO reqVO) {

        // 查询板件的的信息
        return orderPlateMapper.selectOtherPlateList(reqVO.getPlateIdList());

    }







    // 新增补板详情表
    @Override
    public void insertPatching(PatchingReqVO reqVO) {

        PatchDetailDO patchDetailDO = new PatchDetailDO();

        // 循环查询板件的信息，并加入补板详情表中
        for (Long ids : reqVO.getIdList()) {

            PatchingDetailRespVO patchingDetailRespVO = orderPlateMapper.selectPlate(ids);
            String jsonString = JsonUtils.toJsonString(patchingDetailRespVO);

            patchDetailDO.setId(ids);
            patchDetailDO.setOrderId(patchingDetailRespVO.getOrderId());
            patchDetailDO.setArea(patchDetailDO.getArea());
            patchDetailDO.setPlanId(reqVO.getPlanId());
            patchDetailDO.setMachineId(reqVO.getMachineId());

            patchDetailDO.setDetail(jsonString);

            patchDetailMapper.insert(patchDetailDO);

        }


    }







    // 查询所有的机台
    @Override
    public List<MachineDO> searchMachine() {

        return machineMapper.selectList();


    }




}
