package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.libs.common.json.JsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.organisation.SysSupplierExtInvoice;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierExtInvoiceParam;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierExtInvoiceQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysSupplierExtInvoiceDubboConsumerService;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 供应商|商户信息-扩展-发票相关
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-11-29 16:34:49
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-11-29 16:34:49
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "供应商信息-扩展-发票相关", tags = {"供应商信息-扩展-发票相关"})
@Slf4j
@RestController
@RequestMapping("/sysSupplierExtInvoice")
public class SysSupplierExtInvoiceController extends BaseController {

	@Autowired
	private SysSupplierExtInvoiceDubboConsumerService sysSupplierExtInvoiceDubboConsumerService;

	@ApiOperation(value = "分页查询 - 默认不用")
	@RequestMapping(value = "listByObscure", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysSupplierExtInvoice>> pageQuerySysSupplierExtInvoice(@RequestBody PageQueryParam param) {

		log.info("pageQuerySysSupplierExtInvoice:\n{}", JsonUtils.toJson(param));

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

		// TODO 参数合法性判定

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

		return result;
	}

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysSupplierExtInvoice>> pageQueryGenericParamSysSupplierExtInvoice(@RequestBody PageQueryGenericParam<SysSupplierExtInvoiceQueryCriteria> param) {

		log.info("pageQuerySysSupplierExtInvoice:\n{}", JsonUtils.toJson(param));

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

		// TODO 参数合法性判定

		result = sysSupplierExtInvoiceDubboConsumerService.pageQueryGenericParam(param.getConditions());

		return result;
	}

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

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

		// TODO 参数合法性判定

		SysSupplierExtInvoice entity = sysSupplierExtInvoiceDubboConsumerService.findSysSupplierExtInvoiceById(id);

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

		result.setData(data);

		return result;
	}

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

		log.info("addSysSupplierExtInvoice:\n{}", JsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysSupplierExtInvoiceDubboConsumerService.addSysSupplierExtInvoice(param);

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

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

		log.info("updateSysSupplierExtInvoice:\n{}", JsonUtils.toJson(param));

		sysSupplierExtInvoiceDubboConsumerService.updateSysSupplierExtInvoice(param);

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

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

		// TODO 参数合法性判定

		sysSupplierExtInvoiceDubboConsumerService.deleteSysSupplierExtInvoice(id);

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

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

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

		for (long id : ids) {
			sysSupplierExtInvoiceDubboConsumerService.deleteSysSupplierExtInvoice(id);
		}

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

	}

	@ApiOperation(value = "设置默认-会取消之前默认的")
	@RequestMapping(value = "set/default/{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<String> setSysSupplierExtInvoiceDefaultById(@PathVariable("id") long id) {

		// TODO 参数合法性判定

		SysSupplierExtInvoice entity = sysSupplierExtInvoiceDubboConsumerService.findById(id);

		if (null == entity) {
			return new R<>("500", "非法操作", false);
		}

		sysSupplierExtInvoiceDubboConsumerService.updateSysSupplierExtInvoiceSetDefault(entity);

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


}
