package cn.superdesk.uniorder.controller.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.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.accounting.AccountingAppeal;
import cn.superdesk.uniorder.dto.param.accounting.AccountingAppealParam;
import cn.superdesk.uniorder.dubbo.consumer.accounting.AccountingAppealDubboConsumerService;
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: 2020-10-23 17:19:13
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-10-23 17:19:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[对账单]-供应商结算-申诉操作", tags = {"[对账单]-供应商结算-申诉操作"})
@Slf4j
@RestController
@RequestMapping("/accountingAppeal")
public class AccountingAppealController extends BaseController {

    @Autowired
    private AccountingAppealDubboConsumerService accountingAppealDubboConsumerService;

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

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

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

        // TODO 参数合法性判定

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

        return result;
    }

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

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

        // TODO 参数合法性判定

        AccountingAppeal entity = accountingAppealDubboConsumerService.findAccountingAppealById(id);

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

        result.setData(data);

        return result;
    }

    @ApiOperation(value = "按AccountingOrderId查询")
    @RequestMapping(value = "accountingOrderId/{id}", method = RequestMethod.GET)
    @ResponseBody
    public R<List<AccountingAppeal>> getByAccountingOrderId(@PathVariable("id") Long accountingOrderId) {

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

        // TODO 参数合法性判定

        AccountingAppeal entity = accountingAppealDubboConsumerService.findAccountingAppealByAccountingOrderId(accountingOrderId);

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

        result.setData(data);

        return result;
    }


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

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


        // TODO 参数合法性判定

        accountingAppealDubboConsumerService.addAccountingAppeal(param,getToken());

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

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

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

        accountingAppealDubboConsumerService.updateAccountingAppeal(param,getToken());

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

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

        // TODO 参数合法性判定

        accountingAppealDubboConsumerService.deleteAccountingAppeal(id);

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

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

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

        for (long id : ids) {
            accountingAppealDubboConsumerService.deleteAccountingAppeal(id);
        }

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

    }


}
