package com.dayou.service.impl;

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.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);
                    for (BusinessProductionPerformance bpp: performances){
                        if (bpp.getId()==null){
                            bpp.setCheckLoop(performance.getCheckLoop());
                            bpp.setProductionId(bpp.getPId());

                        }
                        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);
    }
}
