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

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.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierIssues;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingUnifiedSupplierIssuesParam;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierIssuesDubboConsumerService;
import cn.superdesk.uniorder.common.numerical.AnIntegerUtils;
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: 2021-02-24 13:44:11
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-02-24 13:44:11
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[对账单]-供应商结算-对账单(问题)", tags = {"[对账单]-供应商结算-对账单(问题)"})
@Slf4j
@RestController
@RequestMapping("/accountingUnifiedSupplierIssues")
public class AccountingUnifiedSupplierIssuesController extends BaseController {

	@Autowired
	private AccountingUnifiedSupplierIssuesDubboConsumerService accountingUnifiedSupplierIssuesDubboConsumerService;


	/**
	 * @param param
	 * @return
	 * @url http://192.168.6.223:8089/web/accountingUnifiedSupplierIssues/init_issue_001
	 * {
	 * "conditions" : {
	 * "isCreateExcel" : 1
	 * }
	 * }
	 */
	@ApiOperation(value = "初始化问题-对账单关联关系-商品Id为NULL")
	@RequestMapping(value = "init_issue_001", method = RequestMethod.POST)
	@ResponseBody
	public R<String> init001AccountingUnifiedSupplierIssuess(@RequestBody PageQueryParam param) {

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

		Integer isCreateExcel = AnIntegerUtils.getInteger(param.getConditions().get("isCreateExcel"));

		// TODO 参数合法性判定

		accountingUnifiedSupplierIssuesDubboConsumerService.init001AccountingUnifiedSupplierIssuess(getToken(), isCreateExcel);

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

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<AccountingUnifiedSupplierIssues>> pageQueryAccountingUnifiedSupplierIssuess(@RequestBody PageQueryParam param) {

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

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

		// TODO 参数合法性判定

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

		return result;
	}

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

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

		// TODO 参数合法性判定

		AccountingUnifiedSupplierIssues entity = accountingUnifiedSupplierIssuesDubboConsumerService.findAccountingUnifiedSupplierIssuesById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		accountingUnifiedSupplierIssuesDubboConsumerService.addAccountingUnifiedSupplierIssues(param);

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

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

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

		accountingUnifiedSupplierIssuesDubboConsumerService.updateAccountingUnifiedSupplierIssues(param);

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

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

		// TODO 参数合法性判定

		accountingUnifiedSupplierIssuesDubboConsumerService.deleteAccountingUnifiedSupplierIssues(id);

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

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

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

		for (long id : ids) {
			accountingUnifiedSupplierIssuesDubboConsumerService.deleteAccountingUnifiedSupplierIssues(id);
		}

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

	}


}
