package cn.superdesk.uniorder.controller.unified;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProject;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProjectTarget;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionSysUser;
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.consumer.sys.SysUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedProjectDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/uproject")
public class UnifiedProjectController extends BaseController {


	@Autowired
	private UnifiedProjectDubboConsumerService unifiedProjectDubboConsumerService;
	@Autowired
	private UnifiedOrderDubboConsumerService   unifiedOrderDubboConsumerService;
	@Autowired
	private SysUserDubboConsumerService        sysUserDubboConsumerService;

	/***
	 * 客户列表分页
	 * @return
	 */
	@PostMapping("/getdata")
	@ResponseBody
	public R<List<UnifiedProjectVo>> getdata() {

		R<List<UnifiedProjectVo>> result = new R<>();

		try {

			Map<String, Object> params = new HashMap<>();

			params.put("project", getString("project"));
			params.put("cususername", getString("cususername"));
			params.put("purusername", getString("purusername"));

			result = unifiedProjectDubboConsumerService.getDataByPage(getToken(), params, getPageNum(), getPageSize());

			if (result != null && result.getData() != null && result.getData().size() > 0) {

				for (UnifiedProjectVo item : result.getData()) {
					Map<String, Object> amountQuery = new HashMap<>();
					amountQuery.put("projectId", item.getProjectId());
					BigDecimal amount = unifiedOrderDubboConsumerService.getAmount(amountQuery);
					item.setAmount(amount);
				}
			}

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/***
	 * 客户列表分页
	 * @return
	 */
	@PostMapping("/getdatatarget")
	@ResponseBody
	public R<List<UnifiedProjectTargetVo>> getdatatarget() {

		R<List<UnifiedProjectTargetVo>> result = new R<>();

		try {

			Map<String, Object> params = new HashMap<>();

			params.put("projectId", getString("projectId"));

			result = unifiedProjectDubboConsumerService.getTargetDataByPage(getToken(), params, getPageNum(), getPageSize());

			if (result != null && result.getData() != null && result.getData().size() > 0) {

				for (UnifiedProjectTargetVo item : result.getData()) {

					Map<String, Object> amountQuery = new HashMap<>();

					amountQuery.put("projectId", item.getProjectId());
					amountQuery.put("yearly", item.getYearly());
					amountQuery.put("monthly", item.getMonthly());

					BigDecimal amount = unifiedOrderDubboConsumerService.getAmount(amountQuery);

					item.setAmount(amount);
				}

			}

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/***
	 * 系统人员列表
	 * @return
	 */
	@PostMapping("/userdata")
	@ResponseBody
	public R<List<SelectOptionSysUser>> userdata() {

		R<List<SelectOptionSysUser>> result = new R<>();

		try {

			Map<String, Object> params = new HashMap<>();
			params.put("virtId", getLong("virtId"));
			List<SelectOptionSysUser> users = sysUserDubboConsumerService.findAll(params);

			result.setSuccess(true);
			result.setData(users);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/***
	 * 编辑项目
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public R<UnifiedProject> edit(UnifiedProject model) {

		R<UnifiedProject> result = new R<>();

		try {

			UnifiedProject project = unifiedProjectDubboConsumerService.updateObj(getToken(), model);

			result.setSuccess(true);
			result.setData(project);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/***
	 * 编辑目标
	 * @return
	 */
	@PostMapping("/edittarget")
	@ResponseBody
	public R<UnifiedProjectTarget> edittarget(UnifiedProjectTargetAdd model) {

		R<UnifiedProjectTarget> result = new R<>();

		try {

			if (model.getProjectId() == null) {
				result.setSuccess(false);
				result.setMessage("项目为空！");
				return result;
			}

			UnifiedProjectTarget target = unifiedProjectDubboConsumerService.updateTarget(getToken(), model);

			result.setSuccess(true);
			result.setData(target);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

}
