package com.dayou.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.dto.TaskRecordDTO;
import com.dayou.entity.BusinessProductionPerformance;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.mapper.BusinessProductionPerformanceMapper;
import com.dayou.service.IBusinessProductionPerformanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.IMajorService;
import com.dayou.service.IUserService;
import com.dayou.utils.LoginContext;
import com.dayou.vo.BusinessProductionPerformanceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务产品绩效分配 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-11-07
 */
@Service
public class BusinessProductionPerformanceServiceImpl extends ServiceImpl<BusinessProductionPerformanceMapper, BusinessProductionPerformance> implements IBusinessProductionPerformanceService {

    @Autowired
    private IMajorService majorService;

    @Autowired
    private IUserService userService;

    @Autowired
    private BusinessProductionPerformanceMapper businessProductionPerformanceMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<BusinessProductionPerformance> selectPage(Page page,BusinessProductionPerformance businessProductionPerformance){
        return this.page(page, new QueryWrapper<BusinessProductionPerformance>(businessProductionPerformance));
    }


    @Override
    public BusinessProductionPerformance detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(TaskRecordDTO<BusinessProductionPerformance> taskRecordDTO){
        BusinessProductionPerformance performance = taskRecordDTO.getTaskData();
        performance.setCheckId(LoginContext.getCurrentUserId());
        return  this.save(performance);
    }

    @Override
    public Boolean update(TaskRecordDTO<BusinessProductionPerformance> taskRecordDTO){
        BusinessProductionPerformance performance = taskRecordDTO.getTaskData();
        performance.setCheckId(LoginContext.getCurrentUserId());
        return  this.updateById(performance);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public BusinessProductionPerformanceVO getDetail(BusinessProductionPerformanceVO performance) {
            BusinessProductionPerformanceVO businessProductionPerformance = new BusinessProductionPerformanceVO();
            if (performance.getBusinessType().equals(MainBusinessEnum.MAJOR_BUSINESS.name())){
                List<BusinessProductionPerformance> performances = businessProductionPerformanceMapper.getMajorDetail(performance);
                if (CollectionUtil.isNotEmpty(performances)){
                    for (BusinessProductionPerformance bpp: performances){
                        if (bpp.getId()==null){
                            bpp.setCheckLoop(performance.getCheckLoop());
                            bpp.setProductionId(bpp.getPId());
                            bpp.setNormalMistakes(new ArrayList<>());
                            bpp.setXNormalMistakes(new ArrayList<>());
                            bpp.setHardMistakes(new ArrayList<>());
                            bpp.setXHardMistakes(new ArrayList<>());
                            bpp.setFatalMistakes(new ArrayList<>());
                            bpp.setXFatalMistakes(new ArrayList<>());
                        }else {
                            bpp.setProductionId(bpp.getPId());
                            JSONObject jsonObject = JSON.parseObject(bpp.getReason());
                            if (jsonObject!=null){
                                bpp.setNormalMistakes((List<String>) jsonObject.get("normalMistakes"));
                                bpp.setXNormalMistakes((List<String>) jsonObject.get("xnormalMistakes"));
                                bpp.setHardMistakes((List<String>) jsonObject.get("hardMistakes"));
                                bpp.setXHardMistakes((List<String>) jsonObject.get("xhardMistakes"));
                                bpp.setFatalMistakes((List<String>) jsonObject.get("fatalMistakes"));
                                bpp.setXFatalMistakes((List<String>) jsonObject.get("xfatalMistakes"));
                            }else{
                                bpp.setNormalMistakes(new ArrayList<>());
                                bpp.setXNormalMistakes(new ArrayList<>());
                                bpp.setHardMistakes(new ArrayList<>());
                                bpp.setXHardMistakes(new ArrayList<>());
                                bpp.setFatalMistakes(new ArrayList<>());
                                bpp.setXFatalMistakes(new ArrayList<>());
                            }

                        }
                        businessProductionPerformance.getInfo().add(bpp);
                    }
                    businessProductionPerformance.setPrincipal(performances.get(0).getPrincipal());
                    businessProductionPerformance.setProductionName(performances.get(0).getProductionName());
                    businessProductionPerformance.setProductionType(performances.get(0).getProductionType());
                    businessProductionPerformance.setProductionNo(performance.getProductionNo());
                }

            }
            return businessProductionPerformance;
    }

    /**
     * 根据业务类型,产品id及checkLoop获取质检信息
     * @param dto 查询条件
     * @return BusinessProductionPerformance
     */
    @Override
    public BusinessProductionPerformance getProductionPerformance(BusinessProductionPerformance dto) {
        return businessProductionPerformanceMapper.getProductionPerformance(dto);
    }
}
