package cn.superdesk.uniorder.controller.operation;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssues;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesParam;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.operation.UnifiedOrderIssuesDubboConsumerService;
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.web.bind.annotation.*;

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


/**
 * @Description: 问题订单
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-01-29 16:47:14
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-01-29 16:47:14
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "订单管理-订单问题", tags = {"订单管理-订单问题"})
@Slf4j
@RestController
@RequestMapping("/unifiedOrderIssues")
public class UnifiedOrderIssuesController extends BaseController {

	@Autowired
	private UnifiedOrderIssuesDubboConsumerService unifiedOrderIssuesDubboConsumerService;

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

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

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

		// TODO 参数合法性判定
		if (null == param.getConditions().getOrderSn() || StringUtils.isBlank(param.getConditions().getOrderSn())) {
			return ResponseResultOuterUtils.createErrorResult("500", "订单编号为空或空串;只支持订单编号精确查询");
		}


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

		return result;
	}

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

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

		// TODO 参数合法性判定

		UnifiedOrderIssues entity = unifiedOrderIssuesDubboConsumerService.findUnifiedOrderIssuesById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		unifiedOrderIssuesDubboConsumerService.addUnifiedOrderIssues(param);

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

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

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

		unifiedOrderIssuesDubboConsumerService.updateUnifiedOrderIssues(param);

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

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

		// TODO 参数合法性判定

		unifiedOrderIssuesDubboConsumerService.deleteUnifiedOrderIssues(id);

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

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

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

		for (long id : ids) {
			unifiedOrderIssuesDubboConsumerService.deleteUnifiedOrderIssues(id);
		}

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

	}


}
