package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
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.dto.param.operationCenter.base.OperationCenterParam;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseParam;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseQueryCriteria;
import cn.superdesk.uniorder.dto.param.organisation.SysVirtQueryParam;
import cn.superdesk.uniorder.dto.vo.sys.SysOrganizationXEnterpriseVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dto.vo.sys.SysVirtVo;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


@Api(value = "企业信息-项目X企业(数据来源-服务之窗)", tags = {"企业信息-项目X企业(数据来源-服务之窗)"})
@Slf4j
@RestController
@RequestMapping("/enterprise")
public class SysEnterpriseController extends BaseController {

	@Autowired
	private SysEnterpriseDubboConsumerService sysEnterpriseDubboConsumerService;


	@ApiOperation(value = "分页查询-企业信息-项目X企业(数据来源-服务之窗)", notes = "积分结算2.1,伪表单整改")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysOrganizationXEnterpriseVo>> pageQuerySysEnterprises(@RequestBody SysEnterpriseQueryCriteria queryCriteria) {

		log.info("SysEnterpriseController -> pageQuerySysEnterprises:\n{}", AnJsonUtils.toJson(queryCriteria));

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

		// TODO 参数合法性判定

		result = sysEnterpriseDubboConsumerService.pageQuery(queryCriteria, getToken());

		return result;
	}

	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<List<SysEnterprise>> getById(@PathVariable("id") long id) {

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

		// TODO 参数合法性判定

		SysEnterprise entity = sysEnterpriseDubboConsumerService.findSysEnterpriseById(id);

		List<SysEnterprise> data = new ArrayList<>();
		data.add(entity);

		result.setData(data);

		return result;
	}

	@ApiOperation(value = "新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public R<String> addSysEnterprise(@RequestBody SysEnterpriseParam param) {

		log.info("addSysEnterprise:\n{}", AnJsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysEnterpriseDubboConsumerService.addSysEnterprise(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateSysEnterprise(@RequestBody SysEnterpriseParam param) {

		log.info("updateSysEnterprise:\n{}", AnJsonUtils.toJson(param));

		sysEnterpriseDubboConsumerService.updateSysEnterprise(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "删除")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteSysEnterprise(@PathVariable("id") long id) {

		// TODO 参数合法性判定

		sysEnterpriseDubboConsumerService.deleteSysEnterprise(id);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "批量删除")
	@RequestMapping(value = "delete_ids", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteIdsSysEnterprise(@RequestParam(value = "ids") long[] ids) {

		log.info(AnJsonUtils.toJson(ids));

		for (long id : ids) {
			sysEnterpriseDubboConsumerService.deleteSysEnterprise(id);
		}

		return new R<>("200", "操作成功", true);

	}

	@ApiOperation(value = "返回 项目X企业 列表")
	@PostMapping("/findAll")
	@ResponseBody
	public R<List<SysOrganizationXEnterpriseVo>> findAll() {

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

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

			params.put("isgenbill", 1);

//			String getDD = getString("DD");
//			String getnotDD = getString("notDD");
//			if(StringUtils.isNotEmpty(getDD)) {
//				params.put("getDD", getDD);
//			}
//			if(StringUtils.isNotEmpty(getnotDD)) {
//				params.put("getnotDD", getnotDD);
//			}

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

			List<SysOrganizationXEnterpriseVo> resultData = sysEnterpriseDubboConsumerService.findSysOrganizationXEnterprise(params);

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

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("系统查找失败！");

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

			e.printStackTrace();
		}

		return result;
	}


	@PostMapping("/findByName")
	@ResponseBody
	public R<List<SysEnterprise>> findByName() {

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

		try {

			List<SysEnterprise> ls = sysEnterpriseDubboConsumerService.findByName(getLong("organizationId"), getString("name"));

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

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("系统查找失败！");

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

			e.printStackTrace();
		}
		return result;
	}

//	@PostMapping("/findAllDD")
//	@ResponseBody
//	public R<List<SysEnterprise>> findAllDD() {
//
//		R<List<SysEnterprise>> result = new R<>();
//
//		try {
//			Map<String, Object> params = new HashMap<>();
//
//			String getDD    = getString("DD");
//			String getnotDD = getString("notDD");
//
//			if (StringUtils.isNotEmpty(getDD)) {
//				params.put("getDD", getDD);
//			}
//
//			if (StringUtils.isNotEmpty(getnotDD)) {
//				params.put("getnotDD", getnotDD);
//			}
//
//			params.put("enterpriseName", getString("enterpriseName"));
//			List<SysEnterprise> ls = sysEnterpriseDubboConsumerService.findSysOrganizationXEnterprise(params);
//
//			result.setSuccess(true);
//			result.setData(ls);
//
//		} catch (Exception e) {
//
//			result.setCode(String.valueOf(e.hashCode()));
//			result.setSuccess(false);
//			result.setMessage("系统查找失败！");
//
//			log.error(ExceptionUtils.getMessage(e));
//			e.printStackTrace();
//		}
//		return result;
//	}

//	@PostMapping("/ddsave")
//	@ResponseBody
//	public R<String> ddsave(Long id) {
//
//		R<String> result = new R<>();
//
//		try {
//			sysEnterpriseDubboConsumerService.addSpecDaiDeLiangHang(id);
//
//			result.setSuccess(true);
//
//		} catch (Exception e) {
//			result.setCode(String.valueOf(e.hashCode()));
//			result.setSuccess(false);
//			result.setMessage("保存失败！");
//
//			log.error(ExceptionUtils.getMessage(e));
//
//			e.printStackTrace();
//		}
//		return result;
//	}

//	@PostMapping("/ddremove")
//	@ResponseBody
//	public R<String> ddremove(Long id) {
//
//		R<String> result = new R<>();
//
//		try {
//			sysEnterpriseDubboConsumerService.deleteDaiDeLiangHang(id);
//			result.setSuccess(true);
//
//		} catch (Exception e) {
//
//			result.setCode(String.valueOf(e.hashCode()));
//			result.setSuccess(false);
//			result.setMessage("保存失败！");
//
//			log.error(ExceptionUtils.getMessage(e));
//
//			e.printStackTrace();
//		}
//
//		return result;
//	}


	@ApiOperation(value = "企业数据列表", notes = "企业数据列表")
	@RequestMapping(value = "getVirtList", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysVirtVo>> getVirtList(@RequestBody @Validated SysVirtQueryParam param) {
		log.info("SysEnterpriseController -> getVirtList:{}", AnJsonUtils.toJson(param));
		R<List<SysVirtVo>> result = new R<>();
		result = sysEnterpriseDubboConsumerService.getVirtList(param);
		return result;
	}

	@ApiOperation(value = "查询企业下拉列表", notes = "")
	@PostMapping("/selectList")
	@ResponseBody
	public R<List<SysSelectVo>> selectList(@RequestBody OperationCenterParam param) {
		if (StringUtils.isBlank(param.getCustomerId())) {
			return new R<>("500", "客户id必传！", false);
		}

		return new R<>(sysEnterpriseDubboConsumerService.selectList(param));
	}
}
