package cn.superdesk.uniorder.controller.marketing.department;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerDetail;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerVo;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedCustomerDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * @Description: 市场管理-企业管理
 * @Author: linjinyu
 * @CreateDate: 7/8/20 8:14 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 7/8/20 8:14 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "市场管理-企业管理 X 区域 (企业与区域的关联关系设置)", tags = {"市场管理-企业管理"})
@Slf4j
@RestController
//@RequestMapping("/ucustomer")
@RequestMapping("/enterprise/manager")
public class SysEnterpriseManagerController extends BaseController {

	@Autowired
	private UnifiedCustomerDubboConsumerService unifiedCustomerDubboConsumerService;
	@Autowired
	private SysEnterpriseDubboConsumerService   sysEnterpriseDubboConsumerService;


	/***
	 * 前端-市场管理-企业管理
	 *
	 * 错错错 20200708前端 彩娇说 前端没用到
	 *
	 * 市场管理-企业管理-列表分页
	 *
	 * @return
	 */
	@ApiOperation(value = "市场管理-企业管理-列表分页")
	@RequestMapping(value = "/getdata", method = RequestMethod.POST)
	@ResponseBody
	public R<List<UnifiedCustomerVo>> getdata() {

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

		try {

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

			params.put("project", getString("project"));
			params.put("virt", getString("virt"));

			params.put("areaId", getLong("areaId"));
			params.put("areaItemId", getLong("areaItemId"));

			params.put("cususerid", getLong("cususerid"));
			params.put("puruserid", getLong("puruserid"));

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

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

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

//					if (item.getCustomerUsers() != null && item.getCustomerUsers().split(",").length > 0) {
//						Map<String, Object> userQuery = new HashMap<>();
//						userQuery.put("ids", item.getCustomerUsers().split(","));
//						List<SysUserSelect> users = sysUserDubboConsumerService.findAll(userQuery);
//						if (users != null && users.size() > 0) {
//							String userStr = "";
//							for (SysUserSelect u : users) {
//								userStr += u.getFullname() + ",";
//							}
//							item.setCustomerUserName(userStr);
//						}
//					}
//					if (item.getPurchaseUsers() != null && item.getPurchaseUsers().split(",").length > 0) {
//						Map<String, Object> userQuery = new HashMap<>();
//						userQuery.put("ids", item.getPurchaseUsers().split(","));
//						List<SysUserSelect> users = sysUserDubboConsumerService.findAll(userQuery);
//						if (users != null && users.size() > 0) {
//							String userStr = "";
//							for (SysUserSelect u : users) {
//								userStr += u.getFullname() + ",";
//							}
//							item.setPurchaseUserName(userStr);
//						}
//					}
				}
			}

		} 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
	 */
	@ApiOperation(value = "市场管理-企业管理-添加客户")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public R<UnifiedCustomer> add(UnifiedCustomerAdd model) {

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

		try {

			SysEnterprise virt = sysEnterpriseDubboConsumerService.findByVirtId(model.getVirtId());

			if (virt == null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("企业不存在！");
				return result;
			}

			UnifiedCustomer getcustomer = unifiedCustomerDubboConsumerService.findById(model.getVirtId());

			if (getcustomer != null) {
				result.setCode(400);
				result.setSuccess(false);
				result.setMessage("企业已存在,不可重复配置！");
				return result;
			}

			UnifiedCustomer customer = unifiedCustomerDubboConsumerService.addObjTran(virt, model, getToken());

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

		} 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
	 */
	@ApiOperation(value = "市场管理-企业管理-编辑客户")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public R<UnifiedCustomer> edit(UnifiedCustomerAdd model) {

		R<UnifiedCustomer> result = new R<>();
		try {

			UnifiedCustomer customer = unifiedCustomerDubboConsumerService.updateObj(model, getToken());

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

		} 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
	 */
	@ApiOperation(value = "市场管理-企业管理-查看详情")
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	@ResponseBody
	public R<UnifiedCustomerDetail> detail(Long virtId) {

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

		try {
			UnifiedCustomerDetail detail = unifiedCustomerDubboConsumerService.getDetail(virtId);

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

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