package cn.superdesk.uniorder.controller.invoice.customer.invoiced;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.invoice.customer.invoiced.InvoiceInvoiced;
import cn.superdesk.uniorder.dto.param.invoice.customer.invoiced.*;
import cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedExcelExportVo;
import cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedHeadSum;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedDubboConsumerService;
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 javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-03-16 17:29:47
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-03-16 17:29:47
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "客户结算-应收账款", tags = {"客户结算-应收账款"})
@Slf4j
@RestController
@RequestMapping("/invoiceInvoiced")
public class InvoiceInvoicedController extends BaseController {

	@Autowired
	private InvoiceInvoicedDubboConsumerService invoiceInvoicedDubboConsumerService;

	@Resource
	private Snowflake snowflake;

	/**
	 * /invoiceInvoiced/list
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "应收账款-列表-分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<InvoiceInvoicedExcelExportVo>> pageQueryInvoiceInvoiceds(@RequestBody PageQueryGenericParam<InvoiceInvoicedQueryCriteria> param) {

		log.info("pageQueryInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(param));

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

		// TODO 参数合法性判定

		result = invoiceInvoicedDubboConsumerService.pageQueryByCriteria(param.getConditions());

		return result;
	}

	/**
	 * http://192.168.6.223:8089/web/invoiceInvoiced/exportInvoiceInvoiceds
	 *
	 * @param criteria
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "应收账款-opt 导出应收账款-新版 20210525")
	@RequestMapping(value = "/exportInvoiceInvoiceds", method = RequestMethod.POST)
	public R<String> exportInvoiceInvoiceds(@RequestBody InvoiceInvoicedQueryCriteria criteria) throws IOException {

		log.info("供应商结算-未对账订单-对账单详情-详情导出 params: \n{}", JSONUtil.toJsonStr(criteria));

		return new R<String>(invoiceInvoicedDubboConsumerService.exportInvoiceInvoiceds(getToken(), criteria));

	}


	/**
	 * /invoiceInvoiced/headSum
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "页头查询")
	@RequestMapping(value = "headSum", method = RequestMethod.POST)
	@ResponseBody
	public R<InvoiceInvoicedHeadSum> headSumInvoiceInvoiceds(@RequestBody PageQueryGenericParam<InvoiceInvoicedQueryCriteria> param) {

		log.info("headSumInvoiceInvoiceds:\n{}", JSONUtil.toJsonStr(param));

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

		// TODO 参数合法性判定

		result = invoiceInvoicedDubboConsumerService.headSumInvoiceInvoiceds(param.getConditions());

		return result;
	}


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

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

		// TODO 参数合法性判定

		InvoiceInvoiced entity = invoiceInvoicedDubboConsumerService.findInvoiceInvoicedById(id);

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

		result.setData(data);

		return result;
	}

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

		log.info("addInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(param));


		// TODO 参数合法性判定

		invoiceInvoicedDubboConsumerService.addInvoiceInvoiced(param);

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

//	@ApiOperation(value = "操作-编辑功能")
//	@RequestMapping(value = "update", method = RequestMethod.POST)
//	@ResponseBody
//	public R<String> updateInvoiceInvoiced(@RequestBody InvoiceInvoicedParam param) {
//
//		log.info("updateInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(param));
//
//		invoiceInvoicedDubboConsumerService.updateInvoiceInvoiced(param);
//
//		return new R<>("200", "操作成功", true);
//	}

	@ApiOperation(value = "操作-编辑功能")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateInvoiceInvoiced(@RequestBody InvoiceInvoicedOpUpdateFieldsForm form) {

		form.setBatchId(snowflake.nextIdStr());

		log.info("updateInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(form));

		invoiceInvoicedDubboConsumerService.updateOpFields(getToken(), form);

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

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

		// TODO 参数合法性判定

		invoiceInvoicedDubboConsumerService.deleteInvoiceInvoiced(id);

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

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

		log.info(JSONUtil.toJsonStr(ids));

		for (long id : ids) {
			invoiceInvoicedDubboConsumerService.deleteInvoiceInvoiced(id);
		}

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

	}

	@ApiOperation(value = "设置备注")
	@RequestMapping(value = "/setRemark", method = RequestMethod.POST)
	@ResponseBody
	public R setRemark(@RequestBody InvoiceInvoicedOpUpdateFieldRamarkForm form) {

		form.setBatchId(snowflake.nextIdStr());

		invoiceInvoicedDubboConsumerService.updateOpRemark(getToken(), form);

		return new R();
	}


	@ApiOperation(value = "调整组织关系")
	@RequestMapping(value = "/batchUpdateFields", method = RequestMethod.POST)
	@ResponseBody
	public R batchUpdateFields(@RequestBody InvoiceInvoicedOpBatchUpdateFieldsForm form) {

		form.setBatchId(snowflake.nextIdStr());

		invoiceInvoicedDubboConsumerService.batchUpdateFields(getToken(), form);

		return new R();
	}


}
