package com.dayou.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.dto.UserShareRate;
import com.dayou.entity.Major;
import com.dayou.entity.MajorProductionAllot;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.MajorProductionAllotMapper;
import com.dayou.service.IMajorProductionAllotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 大中型产品绩效分配 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-02-19
 */
@DS("mbs")
@Service
public class MajorProductionAllotServiceImpl extends ServiceImpl<MajorProductionAllotMapper, MajorProductionAllot> implements IMajorProductionAllotService {

    @Autowired
    private MajorProductionAllotMapper majorProductionAllotMapper;

    @Override
    @SuppressWarnings("unchecked")
    public List<MajorProductionAllot> getList(Long productionId){
        List<MajorProductionAllot> list = majorProductionAllotMapper.getList(productionId);
        if (CollectionUtils.isEmpty(list)){
            Major major = majorProductionAllotMapper.findMembers(productionId);
            List<Long> members = JSON.parseArray(major.getMembers(), Long.class);
            Long principalId = major.getPrincipalId();
            members.add(principalId);
            list = majorProductionAllotMapper.blankAllot(members);
            list.stream().forEach(x->{
                if (x.getUserId().equals(principalId)){
                    x.setIsPrincipal(true);
                }else{
                    x.setIsPrincipal(false);
                }
            });
            Collections.sort(list,(o1,o2)->o2.getIsPrincipal().compareTo(o1.getIsPrincipal()));
        }
        return list;
    }

    @Transactional
    @Override
    public Boolean saveAllot(List<MajorProductionAllot> list) {
        if (CollectionUtils.isNotEmpty(list)){
            Long majorProductionId = list.get(0).getMajorProductionId();
            majorProductionAllotMapper.deleteByMajorProductionId(majorProductionId);
            return this.saveBatch(list);
        }
        return Boolean.FALSE;
    }

    @Override
    public List<UserShareRate> allotRateByDeclareId(Long id) {
        List<UserShareRate> rates = majorProductionAllotMapper.allotRateByDeclareId(id);
        return rates;
    }

    @Override
    public List<UserShareRate> getXList(Long productionId) {
        List<UserShareRate> rates = majorProductionAllotMapper.getXList(productionId);
        return rates;
    }

}
