package cn.superdesk.uniorder.controller.order.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.libs.common.json.JsonUtils;
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.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderGoodsOpSetOperationTaxCodeForm;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderGoodsParam;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderGoodsQueryCriteria;
import cn.superdesk.uniorder.dto.param.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountJdOpSetRemarkForm;
import cn.superdesk.uniorder.dto.vo.order.base.UnifiedOrderGoodsHeadVo;
import cn.superdesk.uniorder.dto.vo.order.base.UnifiedOrderGoodsListItemVo;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderGoodsDubboConsumerService;
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:     2022-07-13 14:07:41
 * @UpdateUser:     linjinyu
 * @UpdateDate:     2022-07-13 14:07:41
 * @UpdateRemark:   修改内容
 * @Version:        1.0
 */
@Api(value = "基础数据管理-订单商品", tags = {"基础数据管理-订单商品"})
@Slf4j
@RestController
@RequestMapping("/unifiedOrderGoods")
public class UnifiedOrderGoodsController extends BaseController {

	@Autowired
	private UnifiedOrderGoodsDubboConsumerService unifiedOrderGoodsDubboConsumerService;






	@ApiOperation(value = "xxx-xxx-op列表")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<UnifiedOrderGoodsListItemVo>> pageQueryGenericParam(
			@RequestBody UnifiedOrderGoodsQueryCriteria queryCriteria) {

		log.info("pageQueryUnifiedOrderGoods:\n{}", JsonUtils.toJson(queryCriteria));

		// TODO 参数合法性判定

		R<List<UnifiedOrderGoodsListItemVo>> result = new R<>();
		result = unifiedOrderGoodsDubboConsumerService.pageQueryGenericParam(queryCriteria, getToken());

		return result;
	}

	@ApiOperation(value = "xxx-xxx-op页头")
	@RequestMapping(value = "head", method = RequestMethod.POST)
	public R<UnifiedOrderGoodsHeadVo> pageQueryHeadGenericParam(
			@RequestBody UnifiedOrderGoodsQueryCriteria queryCriteria) {

		return ResponseResultOuterUtils.createSuccesResult("",
					unifiedOrderGoodsDubboConsumerService
						.queryHeadGenericParam(
								queryCriteria,
								getToken()
						)
		);
	}

	@ApiOperation(value = "xxx-xxx-op列表-op按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<UnifiedOrderGoodsListItemVo> getById(@PathVariable("id") long id) {

		return ResponseResultOuterUtils.createSuccesResult("",
				unifiedOrderGoodsDubboConsumerService
						.findListItemVoById(
								id,
								getToken()
						)
		);
	}

	@ApiOperation(value = "xxx-xxx-op列表-op新增-op提交")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public R<String> addUnifiedOrderGoods(@RequestBody UnifiedOrderGoodsParam param) {

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

		// TODO 参数合法性判定

		int ret = unifiedOrderGoodsDubboConsumerService.addUnifiedOrderGoods(param, getToken());

		if (ret == 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "操作失败");
		} else {
			return ResponseResultOuterUtils.createSuccesResult("操作成功");
		}
	}

	@ApiOperation(value = "xxx-xxx-op列表-op编辑-op提交")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateUnifiedOrderGoods(@RequestBody UnifiedOrderGoodsParam param) {

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

		int ret = unifiedOrderGoodsDubboConsumerService.updateUnifiedOrderGoods(param, getToken());

		if (ret == 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "操作失败");
		} else {
			return ResponseResultOuterUtils.createSuccesResult("操作成功");
		}
	}

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

		// TODO 参数合法性判定

		int ret = unifiedOrderGoodsDubboConsumerService.deleteUnifiedOrderGoods(id, getToken());

		if (ret == 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "操作失败");
		} else {
			return ResponseResultOuterUtils.createSuccesResult("操作成功");
		}
	}

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

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

		int ret = 0;

		for (long id : ids) {
			ret = unifiedOrderGoodsDubboConsumerService.deleteUnifiedOrderGoods(id, getToken());
		}

		if (ret == 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "操作失败");
		} else {
			return ResponseResultOuterUtils.createSuccesResult("操作成功");
		}

	}


}
