package cn.superdesk.uniorder.controller.invoice.common;

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.invoice.common.InvoiceTypeModeConfigOffLine;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigOffLineParam;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigOffLineQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.invoice.common.InvoiceTypeModeConfigOffLineDubboConsumerService;
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: 2022-02-10 17:46:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-02-10 17:46:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统设置-发票种类-所属开票方式-线下开票", tags = {"系统设置-发票种类-所属开票方式-线下开票"})
@Slf4j
@RestController
@RequestMapping("/invoiceTypeModeConfigOffLine")
public class InvoiceTypeModeConfigOffLineController extends BaseController {

	@Autowired
	private InvoiceTypeModeConfigOffLineDubboConsumerService invoiceTypeModeConfigOffLineDubboConsumerService;

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

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

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

		// TODO 参数合法性判定

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

		return result;
	}

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

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

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

		// TODO 参数合法性判定

		result = invoiceTypeModeConfigOffLineDubboConsumerService.pageQueryGenericParam(param.getConditions(), getToken());

		return result;
	}

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

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

		// TODO 参数合法性判定

		InvoiceTypeModeConfigOffLine entity = invoiceTypeModeConfigOffLineDubboConsumerService.findInvoiceTypeModeConfigOffLineById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		invoiceTypeModeConfigOffLineDubboConsumerService.addInvoiceTypeModeConfigOffLine(param, getToken());

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

	@ApiOperation(value = "编辑-成功会自动更新对映-模块X发票种类X发票方式[线下开票]-开通")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateInvoiceTypeModeConfigOffLine(@RequestBody InvoiceTypeModeConfigOffLineParam param) {

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

		invoiceTypeModeConfigOffLineDubboConsumerService.updateInvoiceTypeModeConfigOffLine(param, getToken());

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

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

		// TODO 参数合法性判定

		invoiceTypeModeConfigOffLineDubboConsumerService.deleteInvoiceTypeModeConfigOffLine(id, getToken());

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

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

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

		for (long id : ids) {
			invoiceTypeModeConfigOffLineDubboConsumerService.deleteInvoiceTypeModeConfigOffLine(id, getToken());
		}

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

	}


}
