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.channel.UnifiedChannelProfitSync;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelSync;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedServiceGroup;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionUnifiedChannelService;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionUnifiedChannelSync;
import cn.superdesk.uniorder.dto.vo.unified.*;
import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelProfitSyncDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelProfitXVirtDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelSyncDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedEncourageDubboConsumerService;
import cn.superdesk.uniorder.common.numerical.AnConvertBeanUtil;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 渠道激励 相关
 * @Author: linjinyu
 * @CreateDate: 7/6/20 8:40 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 7/6/20 8:40 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/uencourage")
public class UnifiedEncourageController extends BaseController {

	@Autowired
	private UnifiedEncourageDubboConsumerService unifiedEncourageDubboConsumerService;
	@Autowired
	private UnifiedChannelProfitSyncDubboConsumerService  unifiedChannelProfitSyncDubboConsumerService;
	@Autowired
	private UnifiedChannelProfitXVirtDubboConsumerService unifiedChannelProfitXVirtDubboConsumerService;
	@Autowired
	private UnifiedChannelSyncDubboConsumerService        unifiedChannelSyncDubboConsumerService;




	/***
	 * 渠道激励列表分页
	 * @return
	 */
	@PostMapping("/getpage")
	@ResponseBody
	public R<List<UnifiedEncouragePage>> getPage() {

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

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

			params.put("channelId", getLong("channelId"));
			params.put("channelName", getString("channelName"));

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

			params.put("jlYear", getString("jlYear"));
			params.put("jlMonth", getString("jlMonth"));

			params.put("startime", getString("startime"));
			params.put("endtime", getString("endtime"));

			result = unifiedEncourageDubboConsumerService.getPage(getToken(), params, getPageNum(), getPageSize());

		} 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("/add")
	@ResponseBody
	public R<Object> addObj(UnifiedEncourageParam param) {

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

		try {

			if (param.getChannelId() == null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("请选择渠道业务类型！");
				return result;
			}

			Map<String, Object> resMap = unifiedEncourageDubboConsumerService.addObj(getToken(), param);

			if (resMap.get("code").toString().equals("1")) {

				result.setSuccess(true);
				result.setData(resMap.get("data"));

			} else {

				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());

			}

		} 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<Object> updateObj(UnifiedEncourageParam param) {

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

		try {
			if (param.getChannelId() == null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("请选择渠道业务类型！");
				return result;
			}

			Map<String, Object> resMap = unifiedEncourageDubboConsumerService.updateObj(getToken(), param);

			if (resMap.get("code").toString().equals("1")) {
				result.setSuccess(true);
				result.setData(resMap.get("data"));
			} else {
				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());
			}

		} 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("/getchanneldata")
	@ResponseBody
	public R<List<SelectOptionUnifiedChannelSync>> getChannelData() {

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

		try {

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

			params.put("enabled", 1);
			params.put("channelName", getString("channelName"));

			List<UnifiedChannelSync>             ls                        = unifiedChannelSyncDubboConsumerService.getChannelAll(params);
			List<SelectOptionUnifiedChannelSync> unifiedChannelSyncSelects = AnConvertBeanUtil.copyListBeanToList(ls, SelectOptionUnifiedChannelSync.class);

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

		} 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("/getchannelservicedata")
	@ResponseBody
	public R<List<SelectOptionUnifiedChannelService>> getChannelServiceData() {

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

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

			params.put("channelId", getLong("channelId"));

			List<UnifiedChannelProfitSync>          ls       = unifiedChannelProfitSyncDubboConsumerService.getChannelProfitAll(params);
			List<SelectOptionUnifiedChannelService> returnLs = new ArrayList<>();

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

				for (UnifiedChannelProfitSync item : ls) {

					SelectOptionUnifiedChannelService channelServiceSelect = new SelectOptionUnifiedChannelService();

					channelServiceSelect.setBusinessCode(item.getBusinessCode());

					UnifiedServiceGroup serviceGroup = unifiedEncourageDubboConsumerService.findServiceGroupByCode(item.getBusinessCode());
					channelServiceSelect.setBusinessCodeName(serviceGroup.getGroupName());

					returnLs.add(channelServiceSelect);
				}
			}

			List<SelectOptionUnifiedChannelSync> unifiedChannelSyncSelects = AnConvertBeanUtil.copyListBeanToList(ls, SelectOptionUnifiedChannelSync.class);

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

		} 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("/getservicedata")
	@ResponseBody
	public R<List<SelectOptionUnifiedChannelService>> getServiceData() {

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

		try {
			Map<String, Object>                     params   = new HashMap<>();
			List<SelectOptionUnifiedChannelService> returnLs = new ArrayList<>();

			List<UnifiedServiceGroup> ls = unifiedEncourageDubboConsumerService.getServiceGroupAll(params);

			if (ls != null && ls.size() > 0) {
				for (UnifiedServiceGroup item : ls) {

					SelectOptionUnifiedChannelService channelServiceSelect = new SelectOptionUnifiedChannelService();

					channelServiceSelect.setBusinessCodeName(item.getGroupName());
					channelServiceSelect.setBusinessCode(item.getGroupCode());

					returnLs.add(channelServiceSelect);
				}
			}

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

		} 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("/getpageprofit")
	@ResponseBody
	public R<List<UnifiedDrowMoneyPage>> getPageProfit() {

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

		try {

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

			params.put("channelId", getLong("channelId"));
			params.put("startime", getString("startime"));
			params.put("endtime", getString("endtime"));

			result = unifiedEncourageDubboConsumerService.getPageProfit(getToken(), params, getPageNum(), getPageSize());


		} 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("/profitadd")
	@ResponseBody
	public R<Object> profitAdd(UnifiedDrowMoneyParam param) {

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

		try {
			if (param.getChannelId() == null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("请选择渠道！");
				return result;
			}

			Map<String, Object> resMap = unifiedEncourageDubboConsumerService.profitAdd(getToken(), param);

			if (resMap.get("code").toString().equals("1")) {

				result.setSuccess(true);
				result.setData(resMap.get("data"));

			} else {

				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());
			}

		} 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("/getcancash")
	@ResponseBody
	public R<Object> getCanCash(Long channelId) {

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

		try {
			Map<String, Object> amountProfitQuery = new HashMap<>();

			amountProfitQuery.put("channelId", channelId);

			BigDecimal amountProfit    = unifiedEncourageDubboConsumerService.getAmountProfit(amountProfitQuery);//分润金额
			BigDecimal amountBack      = unifiedEncourageDubboConsumerService.getAmountBack(amountProfitQuery);//回款金额
			BigDecimal amount          = unifiedEncourageDubboConsumerService.getOrderAmount(amountProfitQuery);//金额
			BigDecimal amountEncourage = unifiedEncourageDubboConsumerService.getAmountEncourage(amountProfitQuery);//激励金额
			BigDecimal amountgetcash   = unifiedEncourageDubboConsumerService.getAmountGetCash(amountProfitQuery);

			if (amountProfit == null) {
				amountProfit = new BigDecimal(0);
			}
			if (amountBack == null) {
				amountBack = new BigDecimal(0);
			}
			if (amount == null) {
				amount = new BigDecimal(0);
			}
			if (amountEncourage == null) {
				amountEncourage = new BigDecimal(0);
			}
			if (amountgetcash == null) {
				amountgetcash = new BigDecimal(0);
			}

			double getcash = 0;

			if (amountBack.doubleValue() != 0 && amount.doubleValue() != 0) {
				getcash = amountProfit.doubleValue() * amountBack.doubleValue() / amount.doubleValue() + amountEncourage.doubleValue()-amountgetcash.doubleValue();

			}

			result.setData(getcash);

		} 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("/getchannelpage")
	@ResponseBody
	public R<List<UnifiedChannelProfitSyncPage>> getChannelPage() {

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

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

			params.put("channelName", getString("channelName"));
			params.put("projectName", getString("projectName"));

			result = unifiedChannelProfitSyncDubboConsumerService.getChannelPage(getToken(), params, getPageNum(), getPageSize());

		} 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;
	}

}
