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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.flow.dao.ResultTagetLibMapper;
import com.lz.modules.flow.entity.ResultModel;
import com.lz.modules.flow.entity.ResultTagetLib;
import com.lz.modules.flow.model.ResultTagetLibDto;
import com.lz.modules.flow.req.ResultTagetLibItemReq;
import com.lz.modules.flow.req.ResultTagetLibSearchReq;
import com.lz.modules.flow.service.ResultModelService;
import com.lz.modules.performance.service.ResultTagetLibService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* <p>
* 考核指标库表 服务类
* </p>
*
* @author quyixiao
* @since 2020-10-16
*/

@Service
@Slf4j
public class ResultTagetLibServiceImpl extends ServiceImpl<ResultTagetLibMapper, ResultTagetLib> implements ResultTagetLibService {


    @Autowired
	private ResultTagetLibMapper resultTagetLibMapper;

	@Autowired
	private ResultModelService resultModelService;


	@Override
	public ResultTagetLib selectResultTagetLibById(Long id){
		return resultTagetLibMapper.selectResultTagetLibById(id);
	}



	@Override
	public R insertResultTagetLib(ResultTagetLib resultTagetLib){
		log.info("新增指标{}", resultTagetLib);
		if(resultTagetLib.getModelId().intValue() != 0){
			//判断当前添加的权重有没有可能超过限制公共指标不做限制
			ResultModel resultModel = resultModelService.selectResultModelById(resultTagetLib.getModelId());
			if(resultModel == null){
				return R.error("找不到相关考核模板");
			}
			List<ResultTagetLibDto> resultTagetLibDtos = selectResultTagetLibDtoByModelId(resultModel.getId());
			BigDecimal weight = BigDecimal.ZERO;
			for(int i = 0; i < resultTagetLibDtos.size(); i++){
				ResultTagetLibDto dto = resultTagetLibDtos.get(i);
				weight = weight.add(dto.getWeight());
			}
			weight = weight.add(resultTagetLib.getWeight());
			if(weight.compareTo(resultModel.getWeight()) > 0){
				return R.error("权重之和不能超过" + (resultModel.getWeight().multiply(new BigDecimal(100)) + "%" ));
			}
		}
		resultTagetLibMapper.insertResultTagetLib(resultTagetLib);
		return R.ok().put("data", resultTagetLib);
	}



	@Override
	public int updateResultTagetLibById(ResultTagetLib resultTagetLib){
		return resultTagetLibMapper.updateResultTagetLibById(resultTagetLib);
	}



	@Override
	public int updateCoverResultTagetLibById(ResultTagetLib resultTagetLib){
		return resultTagetLibMapper.updateCoverResultTagetLibById(resultTagetLib);
	}



	@Override
	public int deleteResultTagetLibById(Long id){
		return resultTagetLibMapper.deleteResultTagetLibById(id);
	}

	@Override
	public List<ResultTagetLib> selectResultTagetLibByModelId(Long id){
		return resultTagetLibMapper.selectResultTagetLibByModelId(id);
	}

	@Override
	public PageUtils selectResultTagetLibByReq(ResultTagetLibSearchReq req){
		PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize()).doSelect(
				page -> resultTagetLibMapper.selectByCondition(page, req)
		);
		return pageUtils;
	}

	@Override
	public int deleteResultTagetLibByModelId(Long id){
		log.info("删除模板下面的指标，模板维度id={}", id);
		return resultTagetLibMapper.deleteResultTagetLibByModelId(id);
	}

	@Override
	public int insertResultTagetLibs(List<ResultTagetLib> inserts){
		return resultTagetLibMapper.insertResultTagetLibs(inserts);
	}

	@Override
	public int updateResultTagetLibByIds(List<ResultTagetLib> inserts){
		return resultTagetLibMapper.updateResultTagetLibByIds(inserts);
	}

	@Override
	public List<ResultTagetLibItemReq> selectResultTagetLibByModelReqId(Long id){
		return resultTagetLibMapper.selectResultTagetLibByModelReqId(id);
	}

	@Override
	public  List<ResultTagetLibDto> selectResultTagetLibDtoByModelId(Long id){
		return resultTagetLibMapper.selectResultTagetLibDtoByModelId(id);
	}



}
