package com.lz.modules.flow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.R;
import com.lz.modules.flow.dao.ResultModelMapper;
import com.lz.modules.flow.entity.ResultModel;
import com.lz.modules.flow.entity.ResultTagetLib;
import com.lz.modules.flow.model.ResultModelDto;
import com.lz.modules.flow.req.ResultModelDetailReq;
import com.lz.modules.flow.req.ResultModelItemReq;
import com.lz.modules.flow.req.ResultTagetLibItemReq;
import com.lz.modules.flow.service.ResultModelService;
import com.lz.modules.performance.service.ResultTagetLibService;
import com.lz.modules.sys.entity.app.ResultDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 考核模板表 服务类
* </p>
*
* @author quyixiao
* @since 2020-10-13
*/

@Service
@Slf4j
public class ResultModelServiceImpl extends ServiceImpl<ResultModelMapper, ResultModel> implements ResultModelService {


    @Autowired
	private ResultModelMapper resultModelMapper;

    @Autowired
	private ResultTagetLibService resultTargetLibService;


	private static int maxInsertSQL = 100;


	@Override
	public ResultModel selectResultModelById(Long id){
		return resultModelMapper.selectResultModelById(id);
	}



	@Override
	public Long insertResultModel(ResultModel resultModel){
		return resultModelMapper.insertResultModel(resultModel);
	}



	@Override
	public int updateResultModelById(ResultModel resultModel){
		return resultModelMapper.updateResultModelById(resultModel);
	}



	@Override
	public int updateCoverResultModelById(ResultModel resultModel){
		return resultModelMapper.updateCoverResultModelById(resultModel);
	}



	@Override
	public R deleteResultModelById(Long id){
		//
		log.info("删除考核模板维度{}", id);
		resultTargetLibService.deleteResultTagetLibByModelId(id);
		resultModelMapper.deleteResultModelById(id);
		return R.ok();
	}

	@Override
	public List<ResultModel> selectResultModelByGroupId(Long id){
		return resultModelMapper.selectResultModelByGroupId(id);
	}

	@Override
	public List<ResultModelDto> selectResultDtoByGroupId(Long id){
		return resultModelMapper.selectResultDtoByGroupId(id);
	}

	@Override
	public R deleteResultModelByGroupId(Long id){
		List<ResultModel> resultModels = selectResultModelByGroupId(id);
		for (ResultModel model:resultModels
			 ) {
			deleteResultModelById(model.getId());
		}
		return R.ok();
	}

	@Override
	public R saveDetail(ResultModelDetailReq resultModelDetailReq){
		BigDecimal modelWeight = BigDecimal.ZERO;
		int resultModelOrderBy = 0;

		int delCount = resultModelDetailReq.getModelItems().size();//记录删除的条数。如果为0，那么插入一条记录
		for (ResultModelItemReq itemReq:
				resultModelDetailReq.getModelItems()) {
			if(itemReq.getWeight() != null){
				modelWeight = modelWeight.add(itemReq.getWeight());
			}

			if(modelWeight.compareTo(BigDecimal.ONE) == 1){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
				return R.error("维度权重之和不能能超过100%");
			}
			ResultModel resultModel = new ResultModel();
			BeanUtils.copyProperties(itemReq, resultModel);
			if(resultModel.getMaxCount() == null){
				resultModel.setMaxCount(10000);
			}
			resultModel.setCalculateId(resultModelDetailReq.getCalculateId());
			resultModel.setGradeGroupId(resultModelDetailReq.getGradeGroupId());
			resultModel.setGradeStatus(resultModelDetailReq.getGradeStatus());
			resultModel.setEvaluationGroupId(resultModelDetailReq.getEvaluationGroupId());
			resultModel.setOrderBy(resultModelOrderBy);
			if(resultModel.getWeight() == null){
				resultModel.setWeight(BigDecimal.ZERO);
			}
			if(resultModel.getId() == null){
				resultModelMapper.insertResultModel(resultModel);

			}else{

				resultModelMapper.updateResultModelById(resultModel);

				if(resultModel.getIsDelete() != null && resultModel.getIsDelete().intValue() == 1){
					delCount--;
				}
			}
			if(itemReq.getTagetLibItems() != null){
				int libOrderBy = 0;
				List<ResultTagetLib> inserts = new ArrayList<>();
				List<ResultTagetLib> updates = new ArrayList<>();
				BigDecimal tagLibWeight = BigDecimal.ZERO;
				for (ResultTagetLibItemReq req:itemReq.getTagetLibItems()
				) {
					ResultTagetLib resultTagetLib = new ResultTagetLib();
					BeanUtils.copyProperties(req, resultTagetLib);
					resultTagetLib.setModelId(resultModel.getId());
					resultTagetLib.setOrderBy(libOrderBy);
					if(req.getWeight() != null && (req.getIsDelete() == null || req.getIsDelete().intValue() == 0)){
						tagLibWeight = tagLibWeight.add(req.getWeight());
						if(!resultModel.getWeight().equals(BigDecimal.ZERO) && tagLibWeight.compareTo(resultModel.getWeight()) == 1){
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
							return R.error(resultModel.getName() +
									"维度的指标之和不能大于" + (resultModel.getWeight().multiply(BigDecimal.valueOf(100))) + "%");
						}
					}

					if(resultTagetLib.getId() != null && resultTagetLib.getId().intValue() > 0){
						updates.add(resultTagetLib);
					}else{
						inserts.add(resultTagetLib);
					}
					if(resultTagetLib.getIsDelete() == null || resultTagetLib.getIsDelete().intValue() == 0){
						libOrderBy++;
					}

				}
				if(inserts.size() > 0){
					resultTargetLibService.insertResultTagetLibs(inserts);
				}
				if(updates.size() > 0){
					resultTargetLibService.updateBatchById(updates);
				}

				if(resultModel.getIsDelete() == null || resultModel.getIsDelete().intValue() == 0){
					resultModelOrderBy++;
				}
			}

		}
		/*if(delCount == 0){
			//如果全部被删除，或者没有传入具体维度，那么默认保存一个维度
			//没有传具体的维度
			ResultModel resultModel = new ResultModel();
			resultModel.setCalculateId(resultModelDetailReq.getCalculateId());
			resultModel.setGradeGroupId(resultModelDetailReq.getGradeGroupId());
			resultModel.setGradeStatus(resultModelDetailReq.getGradeStatus());
			resultModel.setEvaluationGroupId(resultModelDetailReq.getEvaluationGroupId());
			resultModel.setOrderBy(resultModelOrderBy);
			resultModelMapper.insertResultModel(resultModel);
			return R.ok();
		}*/
		return R.ok();
	}



}
