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

import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderInvoiceApplyStatusEnum;
import cn.superdesk.uniorder.common.enums.accounting.customer.AccountingOrderCustomerAccountingStatusEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.accounting.score.customer.AccountingScoreCustomer;
import cn.superdesk.uniorder.dubbo.consumer.accounting.customer.AccountingScoreCustomerDubboConsumerService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "积分结算-对账单-状态流转", tags = {"积分结算-对账单-状态流转"})
@Slf4j
@RestController
@RequestMapping("/accounting/score/customer/statement/state")
public class ScoreCustomerStatementStateFieldAccountingStatusController extends BaseController {

    @Autowired
    private AccountingScoreCustomerDubboConsumerService accountingScoreCustomerDubboConsumerService;

    /****************************************** 状态流转类 start ******************************************/

    /**
     * 确认结算
     * <p>
     * http://192.168.1.223:8089/web/dzmain/confirmDz
     */
    @ApiOperation(value = "积分结算-对账单-op确认对账")
    @RequestMapping(value = "/confirmAccounting", method = RequestMethod.POST)
    @ResponseBody
    public R<?> opConfirmAccounting(Long id) {

        R<?> result = new R<>();

        try {
            AccountingScoreCustomer main = accountingScoreCustomerDubboConsumerService.findById(id);

            if (main.getAccountingStatus() != 0) {
                result.setSuccess(false);
                result.setMessage("确认失败,该对账单已操作！");
                return result;
            }

            accountingScoreCustomerDubboConsumerService.opConfirmAccounting(id, getToken());

            result.setSuccess(true);

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 拒绝 结算
     */
    @ApiOperation(value = "积分结算-对账单-op拒绝对账")
    @RequestMapping(value = "/refuseAccounting", method = RequestMethod.POST)
    @ResponseBody
    public R<?> opRefuseAccounting(Long id) {

        R<?> result = new R<>();
        try {

            AccountingScoreCustomer main = accountingScoreCustomerDubboConsumerService.findById(id);

            if (main.getAccountingStatus() != 0) {
                result.setSuccess(false);
                result.setMessage("拒绝失败,该对账单已操作！");
                return result;
            }

            accountingScoreCustomerDubboConsumerService.opRefuseAccounting(id, getToken());

            result.setSuccess(true);

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
        return result;

    }

    /**
     * /accounting/score/customer/statement/state/opAgainAccounting
     * @param id
     * @return
     */
    @ApiOperation(value = "积分结算-对账单-op重新对账")
    @RequestMapping(value = "/opAgainAccounting", method = RequestMethod.POST)
    @ResponseBody
    public R<?> opAgainAccounting(Long id) {

        R<?> result = new R<>();
        try {

            AccountingScoreCustomer accountingScoreCustomerEntity = accountingScoreCustomerDubboConsumerService.findById(id);

            // 要求是 未确认 -> 已确认 mark by linjinyu at 20220321
            if (AccountingOrderCustomerAccountingStatusEnum.已确认.getKey().compareTo(accountingScoreCustomerEntity.getAccountingStatus()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单状态发生变更, 请检查");
                return result;
            }
            if (AccountingOrderCustomerAccountingStatusEnum.已取消.getKey().compareTo(accountingScoreCustomerEntity.getAccountingStatus()) == 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已取消,不可重复操作！");
                return result;
            }
            if (AccountingOrderInvoiceApplyStatusEnum.未申请.getKey().compareTo(accountingScoreCustomerEntity.getInvoiceStatus()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已开票,不可重新对账！");
                return result;
            }
            if (AccountingOrderBackAccountStatusEnum.未回款.getKey().compareTo(accountingScoreCustomerEntity.getBackAccountStatus()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已回款,不可重新对账！");
                return result;
            }

            accountingScoreCustomerDubboConsumerService.opCancelAccounting(id, getToken());

            result.setSuccess(true);

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;

    }

    @ApiOperation(value = "积分对账-对账单-op取消对账")
    @RequestMapping(value = "/opCancelAccounting", method = RequestMethod.POST)
    @ResponseBody
    public R<?> opCancelAccounting(Long id) {

        R<?> result = new R<>();
        try {

            AccountingScoreCustomer main = accountingScoreCustomerDubboConsumerService.findById(id);

            if (main.getAccountingStatus().compareTo(AccountingOrderCustomerAccountingStatusEnum.未确认.getKey()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单状态发生变更, 请检查");
                return result;
            }
            if (main.getAccountingStatus().compareTo(AccountingOrderCustomerAccountingStatusEnum.已取消.getKey()) == 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已取消,不可重复操作！");
                return result;
            }
            if (main.getInvoiceStatus().compareTo(AccountingOrderInvoiceApplyStatusEnum.未申请.getKey()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已开票,不可重新对账！");
                return result;
            }
            if (main.getBackAccountStatus().compareTo(AccountingOrderBackAccountStatusEnum.未回款.getKey()) != 0) {
                result.setSuccess(false);
                result.setMessage("该对账单已回款,不可重新对账！");
                return result;
            }

            accountingScoreCustomerDubboConsumerService.opCancelAccounting(id, getToken());

            result.setSuccess(true);

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;

    }

    /**
     * 删除对账 只能删除已取消的
     */
    @ApiOperation(value = "积分结算-对账单-op删除对账")
    @RequestMapping(value = "/deleteAccounting", method = RequestMethod.POST)
    @ResponseBody
    public R<?> opDeleteAccounting(Long id) {

        R<?> result = new R<>();

        try {

            AccountingScoreCustomer main = accountingScoreCustomerDubboConsumerService.findById(id);

            if (main.getAccountingStatus() == -1) {

                accountingScoreCustomerDubboConsumerService.opDeleteAccounting(id, getToken());

                result.setSuccess(true);

            } else {
                result.setSuccess(false);
                result.setMessage("不能删除 未取消结算单");
                return result;
            }


        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

            log.error(ExceptionUtils.getMessage(e));

            e.printStackTrace();

        }

        return result;

    }

    /****************************************** 状态流转类   end ******************************************/
}
