package cn.superdesk.uniorder.dubbo.provider.unified;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProject;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProjectTarget;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedProjectTargetAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedProjectTargetVo;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedProjectVo;
import cn.superdesk.uniorder.dubbo.facade.unified.DubboUnifiedProjectService;
import cn.superdesk.uniorder.service.unified.UnifiedProjectService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

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

@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboUnifiedProjectServiceImpl implements DubboUnifiedProjectService {

	@Autowired
	private UnifiedProjectService projectService;

	@Override
	public UnifiedProject updateObj(UnifiedProject model) {

		projectService.updateByPrimaryKeySelective(model);
		return model;
	}

	@Override
	public UnifiedProjectTarget updateTarget(UnifiedProjectTargetAdd model, LoginSysUser loginSysUser) {

		UnifiedProjectTarget target = new UnifiedProjectTarget();
		target.setCreateTime(DateUtil.date());
		target.setCreateUserId(loginSysUser.getSysUserId());
		target.setKpiTarget(new BigDecimal(model.getKpi()));
		target.setOkrTarget(new BigDecimal(model.getOkr()));
		String[] dt = model.getMonth().split("-");
		target.setYearly(Integer.parseInt(dt[0]));
		target.setMonthly(Integer.parseInt(dt[1]));
		target.setProjectId(model.getProjectId());
		target.setUpdateTime(DateUtil.date());
		target.setUpdateUserId(loginSysUser.getSysUserId());

		return projectService.updateTarget(target);
	}

	@Override
	public R<List<UnifiedProjectVo>> getDataByPage(Map<String, Object> params, int pageNum,
	                                               int pageSize) {

		return projectService.getDataByPage(params, pageNum, pageSize);
	}

	@Override
	public R<List<UnifiedProjectTargetVo>> getTargetDataByPage(Map<String, Object> params, int pageNum,
	                                                           int pageSize) {

		return projectService.getTargetDataByPage(params, pageNum, pageSize);
	}
}
