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


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cf.imes.framework.common.enums.OrderPackageTypeEnum;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.manage.controller.admin.pack.vo.OrderPartsReqVO;
import com.cf.imes.module.manage.controller.admin.pack.vo.PackDetailsReqVO;
import com.cf.imes.module.manage.controller.admin.pack.vo.PlateDetailsRespVO;
import com.cf.imes.module.manage.controller.admin.process.vo.ProcessRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.OrderPackageVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.PackageDetailsRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.PrintOrderRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.*;
import com.cf.imes.module.manage.controller.admin.query.vo.salary.PriceRatePageReqVO;
import com.cf.imes.module.manage.controller.admin.query.vo.salary.PriceRateReqVO;
import com.cf.imes.module.manage.dal.dataobject.process.OrderDO;
import com.cf.imes.module.manage.dal.dataobject.query.OrderPackageDO;
import com.cf.imes.module.manage.dal.mysql.pack.OrderPackageMapper;
import com.cf.imes.module.manage.dal.mysql.process.ProcessStepItemMapper;
import com.cf.imes.module.manage.dal.mysql.process.ProcessStepMapper;
import com.cf.imes.module.manage.dal.mysql.query.*;
import com.cf.imes.module.manage.util.page.PageHelperUtil;
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;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;


@Service
@Validated
public class ProductionStatusServiceImpl implements ProductionStatusService {

    @Resource
    private ProductionStatusMapper productionStatusMapper;

    @Resource
    private ProcessStepMapper processStepMapper;

    @Resource
    private ProcessStepItemMapper processStepItemMapper;


    @Resource
    private OrderPackageMapper orderPackageMapper;


    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private OrderPartsMapper orderPartsMapper;



    // 生产状态分页查询
    @Override
    public PageResult<OrderDO> getProductionPage(OrderPlatePageReqVO pageReqVO) {

        return productionStatusMapper.selectProductionPage(pageReqVO);

    }



    // 查看生产单下的加工的工序组
    @Override
    public List<ProductionGroupVO> selectProduction(Long orderNo) {

        List<ProductionGroupVO> productionGroupVOS = processStepMapper.selectProducitonList(orderNo,getUserOrganId());

        for (int i = 1; i < productionGroupVOS.size(); i++) {
            productionGroupVOS.get(i).setGroupName(null);
        }

        return productionGroupVOS;
    }





    // 查询板材信息的组合名称
    @Override
    public List<OrderModuleVO> selectPlateName(OrderModuleReqVO reqVO) {

        return orderItemMapper.selectCombinationName(reqVO.getOrderNo());

    }




    // 查看板材信息分页
    @Override
    public PageResult<OrderPlateRespVO> getOrderPlatePage(PlatePageVO pageReqVO) {


        PageHelper.startPage(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<OrderPlateRespVO> orderPlateRespVOS = orderItemMapper.selectPlateList(pageReqVO);
        PageInfo<OrderPlateRespVO> pageInfo = new PageInfo<>(orderPlateRespVOS);
        PageResult<OrderPlateRespVO> priceRateReqVOPageResult = PageHelperUtil.convertToPageResult(pageInfo);

        return priceRateReqVOPageResult;

    }




    // 计件工资分页查询
    @Override
    public PageResult<PriceRateReqVO> getSalaryPage(PriceRatePageReqVO pageReqVO) {

        PageDTO<PriceRateReqVO> page = new PageDTO<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());

        IPage<PriceRateReqVO> priceRateReqVOS = processStepMapper.selectSalaryList(page,pageReqVO,getUserOrganId());


        if (CollectionUtil.isEmpty(priceRateReqVOS.getRecords())) {
            return new PageResult<>();
        }


        return new PageResult<>(priceRateReqVOS.getRecords(), priceRateReqVOS.getTotal());


    }




    // 打包查询分页
    @Override
    public PageResult<OrderDO> queryPackage(OrderPlatePageReqVO pageReqVO) {

        return productionStatusMapper.selectProductionPage(pageReqVO);

    }




    // 生产单打包完成包裹查看
    @Override
    public List<OrderPackageVO> getPackageList(Long orderNo) {

//        List<OrderPackageDO> orderPackageDOS = orderPackageMapper.selectByOrderNo(orderNo);

//        return BeanUtils.toBean(orderPackageDOS, OrderPackageVO.class);

        return null;
    }




    // 查看包裹详情
    @Override
    public PageResult<PackageDetailsRespVO> queryPackageDetails(PackDetailsReqVO reqVO) {

        PageDTO<PackageDetailsRespVO> page = new PageDTO<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 查看配件包裹
        if(reqVO.getType().equals(OrderPackageTypeEnum.PARTS.getType()) || reqVO.getType().equals(OrderPackageTypeEnum.OTHER_ACCESSORIES.getType())){


            // 查看配件包裹
            IPage<PackageDetailsRespVO> packageDetailsRespVOS = orderPartsMapper.selectParts(page,reqVO.getPackageId(),getUserOrganId());
            if (CollectionUtil.isEmpty(packageDetailsRespVOS.getRecords())) {
                return new PageResult<>();
            }

            return new PageResult<>(packageDetailsRespVOS.getRecords(), packageDetailsRespVOS.getTotal());

        }



        // 查看板材包裹
        IPage<PackageDetailsRespVO> packageDetailsRespVOS = orderItemMapper.selectByPackageId(page,reqVO.getPackageId(),getUserOrganId());


        if (CollectionUtil.isEmpty(packageDetailsRespVOS.getRecords())) {
            return new PageResult<>();
        }


        return new PageResult<>(packageDetailsRespVOS.getRecords(), packageDetailsRespVOS.getTotal());

    }




    // 打印订单
    @Override
    public PrintOrderRespVO printOrder(Long orderNo) {

        return productionStatusMapper.selectPrintOrder(orderNo,getUserOrganId());

    }




    // 导出缺板
    @Override
    public PageResult<PlateDetailsRespVO> getMissingBoard(OrderPartsReqVO pageReqVO) {


        PageDTO<PlateDetailsRespVO> page = new PageDTO<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());

        IPage<PlateDetailsRespVO> plateDetailsRespVOS = orderPackageMapper.selectMissingBoard(page,pageReqVO.getOrderNo(),getUserOrganId());

        if (CollectionUtil.isEmpty(plateDetailsRespVOS.getRecords())) {
            return new PageResult<>();
        }


        return new PageResult<>(plateDetailsRespVOS.getRecords(), plateDetailsRespVOS.getTotal());


    }




}
