package cn.superdesk.uniorder.controller._patch.accounting;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.redis.RedisQueueDubboConsumerService;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
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.util.List;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/1/7 下午3:57
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/1/7 下午3:57
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/patch/accounting/unified/supplier/x")
public class AccountingUnifiedSupplierXCheckAndFixController {

	@Autowired
	private AccountingUnifiedSupplierDubboConsumerService accountingUnifiedSupplierDubboConsumerService;
	@Resource
	private RedisQueueDubboConsumerService                redisQueueDubboConsumerService;


	/****************************************** 正方向 start ******************************************/
	/**
	 * 超时不用管 能正常运行
	 * http://192.168.1.223:8089/web/patch/accounting/unified/supplier/x/run
	 * 供应商 patchXCheckAndFix
	 * <p>
	 * 1. 首先 输入的 供应商对账单 SN ! 已确认 ！不存在
	 * <p>
	 * 2. 对账单 关联关系 去 AccountingUnifiedSupplierXGoods 找关系
	 * 关联关系 不存在 问题对账单
	 * 关联关系 Size
	 * <p>
	 * 3. 对账单 关联 商品 通过 存的 AccountingUnifiedSupplierXGoods 分别过滤出 订单 与 商品
	 * <p>
	 * 4. 过滤出 问题数据 已经生成对账单 但状态还是未对账
	 * 关联商品 供应商对账状态-未对账 不存在 不需要修正
	 * 关联商品 供应商对账状态-未对账 存在 Size
	 * <p>
	 * 5. 修正状态
	 * 商品-供应商 对账状态-已对账
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "patch")
	@RequestMapping(value = "run", method = RequestMethod.POST)
	@ResponseBody
	public R<String> run(@RequestBody PageQueryParam param) {

		R<String> response = new R<>();

		// TODO 参数合法性判定

		String accountingOrderSn = null;

		if (null != param.getConditions().get("accountingOrderSn")) {
			accountingOrderSn = param.getConditions().get("accountingOrderSn").toString();
		}

		String report = accountingUnifiedSupplierDubboConsumerService.patchXCheckAndFix(accountingOrderSn);

		response.setData(report);

		return response;
	}

	/**
	 * 批量
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "patch")
	@RequestMapping(value = "runBatch", method = RequestMethod.POST)
	@ResponseBody
	public R<String> runBatch(@RequestBody PageQueryParam param) {

		R<String> response = new R<>();

		// TODO 参数合法性判定

		String accountingOrderSns = null;

		if (null != param.getConditions().get("accountingOrderSns")) {
			accountingOrderSns = param.getConditions().get("accountingOrderSns").toString();
		}

		List<String> accountingOrderSnList = AnStringUtils.stringArrayConvertToStringList(accountingOrderSns, ",");

		log.info("/patch/accounting/unified/supplier/x/runBatch {}", AnJsonUtils.toJson(accountingOrderSnList));

		accountingOrderSnList.forEach(accountingOrderSn -> {
			redisQueueDubboConsumerService.pushMessage2CHANNEL_ACCOUNTING_UNIFIED_SUPPLIER(accountingOrderSn);
		});

		return response;

	}


}
