package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.exception.constants.ExceptionMsg;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExtAccounting;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseExtAccountingParam;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseExtAccountingDubboConsumerService;
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.ArrayList;
import java.util.List;


/**
 * @Description: 企业信息-扩展-对账相关
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-08-20 20:53:39
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-08-20 20:53:39
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "企业信息-扩展-对账相关 账期之类", tags = {"企业信息-扩展-对账相关 账期之类"})
@Slf4j
@RestController
@RequestMapping("/sysEnterpriseExtAccounting")
public class SysEnterpriseExtAccountingController extends BaseController {

	@Autowired
	private SysEnterpriseExtAccountingDubboConsumerService sysEnterpriseExtAccountingDubboConsumerService;


	@ApiOperation(value = "初始化")
	@RequestMapping(value = "init", method = RequestMethod.POST)
	@ResponseBody
	public R<String> initSysEnterpriseExtAccounting(@RequestBody SysEnterpriseExtAccountingParam param) {

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


		// TODO 参数合法性判定

		sysEnterpriseExtAccountingDubboConsumerService.initSysEnterpriseExtAccounting(param);

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


//企业账期优化
//优化内容:添加回款规则,用于判断账单是否回款预期
//1. 回款规则
//	支持选择3种选择方式 rule_of_account
//		1. 账单确认日期 (+ 指定天数)
//		2. 寄票日期 (+ 指定天数)
//		3. 指定日期 (每月?号)
//备注:选择指定日期时显示:
// "设置日期,以日为单位进行设置",隐藏回款周期文本框;
// 反之选择另外2种方式,隐藏"设置日期"项,显示回款周期项; 字段 day_of_month
//根据设置的回款规则+设置的回款周期作为客户结算的账单是否逾期 字段 account_period
//2. 添加邮箱字段,用于客户创建账单时接收邮箱的账号,非必填 --- 弃用

//	ALTER TABLE `sys_enterprise_ext_accounting` CHANGE `account_period` `account_period` INT(10) NOT NULL DEFAULT '0' COMMENT '账期-会计期间';
//	ALTER TABLE `sys_enterprise_ext_accounting`
//	ADD `rule_of_account` INT(10) NULL DEFAULT '0' AFTER `out_virt_id`,
//	ADD `day_of_month` INT(10) NULL DEFAULT '0' AFTER `rule_of_account`;

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysEnterpriseExtAccounting>> pageQuerySysEnterpriseExtAccountings(@RequestBody PageQueryParam param) {

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

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

		// TODO 参数合法性判定

		result = sysEnterpriseExtAccountingDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

		return result;
	}

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

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

		// TODO 参数合法性判定

		SysEnterpriseExtAccounting entity = sysEnterpriseExtAccountingDubboConsumerService.findSysEnterpriseExtAccountingById(id);

		if (null == entity) {
			result.setSuccess(false);
			result.setMessage(ExceptionMsg.RECORD_NOT_EXIST.getMsg());
		} else {
			List<SysEnterpriseExtAccounting> data = new ArrayList<>();
			data.add(entity);
			result.setData(data);
		}


		return result;
	}

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

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


		// TODO 参数合法性判定

		sysEnterpriseExtAccountingDubboConsumerService.addSysEnterpriseExtAccounting(param);

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

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

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

		sysEnterpriseExtAccountingDubboConsumerService.updateSysEnterpriseExtAccounting(param);

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

	@ApiOperation(value = "新增或保存")
	@RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public R<String> saveOrUpdateSysEnterpriseExtAccounting(@RequestBody SysEnterpriseExtAccountingParam param) {

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

		sysEnterpriseExtAccountingDubboConsumerService.saveOrUpdateSysEnterpriseExtAccounting(param, getToken());

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

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

		// TODO 参数合法性判定

		sysEnterpriseExtAccountingDubboConsumerService.deleteSysEnterpriseExtAccounting(id);

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

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

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

		for (long id : ids) {
			sysEnterpriseExtAccountingDubboConsumerService.deleteSysEnterpriseExtAccounting(id);
		}

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

	}


}
