package com.dayou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dayou.common.BaseEntity;
import com.dayou.dto.TaskRecordDTO;
import com.dayou.entity.Personal;
import com.dayou.entity.PersonalCalculate;
import com.dayou.entity.PersonalExample;
import com.dayou.entity.PersonalTarget;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.PersonalCalculateMapper;
import com.dayou.mapper.PersonalExampleMapper;
import com.dayou.service.IPersonalCalculateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.IPersonalExampleService;
import com.dayou.service.IPersonalTargetService;
import com.dayou.utils.PersonalUtils;
import com.dayou.vo.CompareExampleItemVO;
import com.dayou.vo.CompareExampleVO;
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.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 个贷可比实例算价表 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-03-01
 */
@Service
public class PersonalCalculateServiceImpl extends ServiceImpl<PersonalCalculateMapper, PersonalCalculate> implements IPersonalCalculateService {

    @Autowired
    private IPersonalExampleService personalExampleService;

    @Autowired
    private IPersonalCalculateService personalCalculateService;

    @Autowired
    private PersonalCalculateMapper personalCalculateMapper;

    @Autowired
    private PersonalExampleMapper personalExampleMapper;

    @Autowired
    private IPersonalTargetService personalTargetService;

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


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


    @Override
    public Boolean update(PersonalCalculate personalCalculate){
        return  this.updateById(personalCalculate);
    }

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

    @Transactional
    @Override
    public Boolean calculate(TaskRecordDTO<CompareExampleVO> taskRecordDTO) {
        CompareExampleVO compareExampleVO = taskRecordDTO.getTaskData();
        Long calculateId = compareExampleVO.getCalculateId();
        if (calculateId!=null){
            //删除历史计算记录
            personalCalculateMapper.realDelete(calculateId);
            personalExampleMapper.realDelete(compareExampleVO.getTargetId());
        }
        PersonalCalculate personalCalculate = BeanUtil.copyProperties(compareExampleVO, PersonalCalculate.class);
        this.save(personalCalculate);
        personalTargetService.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,compareExampleVO.getTargetId())
                .set(PersonalTarget::getAcreage,compareExampleVO.getAcreage()).set(PersonalTarget::getAmount,compareExampleVO.getAmount()).set(PersonalTarget::getPrice,compareExampleVO.getPrice()));
        List<PersonalExample> personalExamples = PersonalUtils.example2Entity(compareExampleVO.getTargetId(), personalCalculate.getId(), compareExampleVO.getAnalysisData());
        return personalExampleService.saveBatch(personalExamples);
    }

    @Override
    public CompareExampleVO calculateResult(Long targetId) {
        List<PersonalExample> examples = personalExampleService.list(new LambdaQueryWrapper<PersonalExample>().eq(PersonalExample::getTargetId, targetId).eq(BaseEntity::getDeleted, Boolean.FALSE));
        if (examples.size()!=4){
            return null;
        }else {
            PersonalCalculate calculateParams = personalCalculateService.getById(examples.get(0).getCalculateId());
            CompareExampleVO compareExampleVO = BeanUtil.copyProperties(calculateParams, CompareExampleVO.class);
            compareExampleVO.setTargetId(targetId);
            compareExampleVO.setCalculateId(examples.get(0).getCalculateId());
            List<CompareExampleItemVO> items = PersonalUtils.entity2Example(examples);
            compareExampleVO.setAnalysisData(items);
            return compareExampleVO;
        }
    }
}
