package com.zfire.mall.manager.controller.customerFront;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.front.*;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.front.CustomerCostBillOrderLogic;
import com.zfire.mall.manager.logic.front.CustomerTransferOrderLogic;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author changelin
 */
@Slf4j
@RestController
@Api(value = "嘉贤物流仓储账单API", tags = {"嘉贤物流仓储账单API"})
@RequestMapping(value = "/customer/costBill", produces = "application/json; charset=utf-8")
public class CostBillController {

    @Autowired
    CustomerCostBillOrderLogic customerCostBillOrderLogic;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation("商家物流费用账单")
    public ResponseHelper<IPage<CostBillListVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CostBillListVO> stockBeanIPage = customerCostBillOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CostBillListVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("商家物流费用账单导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CostBillListVO> stockBeanIPage = customerCostBillOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("子商户物流费用账单")
    public ResponseHelper<IPage<CostBillListVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CostBillListVO> stockBeanIPage = customerCostBillOrderLogic.listExportV2(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CostBillListVO>() {});
    }

    @PostMapping("/listV2/export")
    @ApiOperation("子商户物流费用账单导出")
    public void listExportV2(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CostBillListVO> stockBeanIPage = customerCostBillOrderLogic.listExportV2(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/listMerchant")
    @ApiOperation("商家对账账单")
    public ResponseHelper<IPage<CostMerchantListVO>> listMerchant(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CostMerchantListVO> stockBeanIPage = customerCostBillOrderLogic.listMerchant(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CostMerchantListVO>() {});
    }

    @PostMapping("/listMerchant/export")
    @ApiOperation("商家对账账单单导出")
    public void listMerchantExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CostMerchantListVO> stockBeanIPage = customerCostBillOrderLogic.listMerchant(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @ZfireList
    @PostMapping("/listSales")
    @ApiOperation("销司物流费用账单")
    public ResponseHelper<IPage<CostSalesListVO>> listSales(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CostSalesListVO> stockBeanIPage = customerCostBillOrderLogic.listSales(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CostSalesListVO>() {});
    }

    @PostMapping("/listSales/export")
    @ApiOperation("销司物流费用账单导出")
    public void listSalesExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CostSalesListVO> stockBeanIPage = customerCostBillOrderLogic.listSales(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/merchantCheck")
    @ApiOperation("商家已对账")
    public ResponseHelper<String> merchantCheck(@ApiParam(required = false, value = "商家对账单编码") @RequestParam(required = false) List<String> merchantCheckCode, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_FRONT+adminUser.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerCostBillOrderLogic.merchantCheck(merchantCheckCode);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }

    }



    @PostMapping("/salesCheck")
    @ApiOperation("销司已对账")
    public ResponseHelper<String> add(@ApiParam(required = false, value = "salesCheckCode") @RequestParam(required = false) List<String> salesCheckCode, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_FRONT+adminUser.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerCostBillOrderLogic.salesCheck(salesCheckCode);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }

    }



    @PostMapping("/ticketGoToReview")
    @ApiOperation("异常工单递交到审核状态")
    public ResponseHelper<String> ticketGoToReview(@RequestBody  CreateTicketBill createTicketBill, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_FRONT+adminUser.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerCostBillOrderLogic.ticketGoToReview(createTicketBill);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }

    }



    @ZfireList
    @PostMapping("/listTicket")
    @ApiOperation("异常工单列表")
    public ResponseHelper<IPage<CostTicketListVO>> listTicket(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CostTicketListVO> stockBeanIPage = customerCostBillOrderLogic.listTicket(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CostTicketListVO>() {});
    }

    @PostMapping("/listTicket/export")
    @ApiOperation("异常工单列表导出")
    public void listTicketExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CostTicketListVO> stockBeanIPage = customerCostBillOrderLogic.listTicket(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


}
