package com.fjhx.mes.service.production.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.production.po.ProductionProcesses;
import com.fjhx.mes.entity.production.po.ProductionReporting;
import com.fjhx.mes.entity.production.po.ProductionReportingDetail;
import com.fjhx.mes.entity.production.po.ProductionTask;
import com.fjhx.mes.entity.production.vo.ProductionReportingDetailVo;
import com.fjhx.mes.mapper.production.ProductionReportingMapper;
import com.fjhx.mes.service.production.ProductionProcessesService;
import com.fjhx.mes.service.production.ProductionReportingDetailService;
import com.fjhx.mes.service.production.ProductionReportingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.mes.service.production.ProductionTaskService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.mes.entity.production.vo.ProductionReportingVo;
import com.fjhx.mes.entity.production.dto.ProductionReportingSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.mes.entity.production.dto.ProductionReportingDto;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 生产报工 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-03-30
 */
@Service
public class ProductionReportingServiceImpl extends ServiceImpl<ProductionReportingMapper, ProductionReporting> implements ProductionReportingService {
    @Autowired
    ProductionReportingDetailService productionReportingDetailService;
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    ProductionProcessesService productionProcessesService;
    @Autowired
    ProductionTaskService productionTaskService;

    @Override
    public Page<ProductionReportingVo> getPage(ProductionReportingSelectDto dto) {
        IWrapper<ProductionReporting> wrapper = getWrapper();
        wrapper.orderByDesc("pr", ProductionReporting::getId);
        wrapper.like("pt.code",dto.getKeyword());
        Page<ProductionReportingVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<ProductionReportingVo> records = page.getRecords();
        UserUtil.assignmentNickName(records,ProductionReporting::getCreateUser,ProductionReportingVo::setOperatorName);
        return page;
    }

    @Override
    public ProductionReportingVo detail(Long id) {
        ProductionReportingVo productionReporting = baseMapper.detail(id);
        ProductInfo productInfo = productInfoService.getById(productionReporting.getProductId());
        if(ObjectUtil.isNotEmpty(productInfo)){
            productionReporting.setProductName(productInfo.getName());
        }
        Map<Long, String> nickNameMapByIds = UserUtil.getNickNameMapByIds(Arrays.asList(productionReporting.getPersonLiableId()));
        if(ObjectUtil.isNotEmpty(nickNameMapByIds)) {
            String name = nickNameMapByIds.get(productionReporting.getPersonLiableId());
            productionReporting.setPersonLiableName(name);
        }

        //赋值报工明细
        List<ProductionReportingDetail> productionReportingDetailList = productionReportingDetailService.list(q -> q.eq(ProductionReportingDetail::getProductionReportingId, productionReporting.getId()));
        List<ProductionReportingDetailVo> productionReportingDetailVos = BeanUtil.copyToList(productionReportingDetailList, ProductionReportingDetailVo.class);
        //赋值工序名称
        List<Long> ProductionProcessesIds = productionReportingDetailList.stream().map(ProductionReportingDetail::getProductionProcessesId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(ProductionProcessesIds)) {
            List<ProductionProcesses> productionProcessesList = productionProcessesService.listByIds(ProductionProcessesIds);
            Map<Long, ProductionProcesses> productionProcessesMap = productionProcessesList.stream().collect(Collectors.groupingBy(ProductionProcesses::getId,
                    Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
            for (ProductionReportingDetailVo productionReportingDetailVo : productionReportingDetailVos) {
                Long productionProcessesId = productionReportingDetailVo.getProductionProcessesId();
                ProductionProcesses productionProcesses = productionProcessesMap.get(productionProcessesId);
                if (ObjectUtil.isNotEmpty(productionProcesses)) {
                    productionReportingDetailVo.setProductionProcessesName(productionProcesses.getName());
                }
            }
            productionReporting.setProductionReportingDetailVoList(productionReportingDetailVos);
        }
        return productionReporting;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ProductionReportingDto productionReportingDto) {
        this.save(productionReportingDto);
        //保存报工明细
        List<ProductionReportingDetail> productionReportingDetailList = productionReportingDto.getProductionReportingDetailList();
        for (ProductionReportingDetail productionReportingDetail : productionReportingDetailList) {
            productionReportingDetail.setProductionReportingId(productionReportingDto.getId());
        }
        productionReportingDetailService.saveBatch(productionReportingDetailList);
        Long productionTaskId = productionReportingDto.getProductionTaskId();
        //设置生产任务状态为进行中
        ProductionTask byId = productionTaskService.getById(productionTaskId);
        byId.setStatus(1);
        productionTaskService.updateById(byId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ProductionReportingDto productionReportingDto) {
        this.updateById(productionReportingDto);
        //保存报工明细
        List<ProductionReportingDetail> productionReportingDetailList = productionReportingDto.getProductionReportingDetailList();
        for (ProductionReportingDetail productionReportingDetail : productionReportingDetailList) {
            productionReportingDetail.setProductionReportingId(productionReportingDto.getId());
        }
        productionReportingDetailService.updateBatchById(productionReportingDetailList);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
