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.ApiNotAuth;
import com.zfire.mall.manager.annotation.Sign;
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.CustomerTransferOrderLogic;
import com.zfire.mall.manager.plus.entity.CustomerTransferOrder;
import com.zfire.mall.manager.utils.CommonUtils;
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 org.springframework.web.multipart.MultipartFile;

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

/**
 * @author changelin
 */
@Slf4j
@RestController
@Api(value = "嘉贤调拨单管理", tags = {"嘉贤调拨单管理"})
@RequestMapping(value = "/customer/transfer", produces = "application/json; charset=utf-8")
public class CustomerTransferOrderController {

    @Autowired
    CustomerTransferOrderLogic customerTransferOrderLogic;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation("经销商调拨单列表")
    public ResponseHelper<IPage<CustomerTransferOrderBean>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerTransferOrderBean> stockBeanIPage = customerTransferOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CustomerTransferOrderBean>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("经销商调拨单列表")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerTransferOrderBean> stockBeanIPage = customerTransferOrderLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @PostMapping("/add")
    @ApiOperation("新增调拨单")
    public ResponseHelper add(@RequestBody CustomerTransferOrderAddOrUp customerTransferOrderAddOrUp, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_TRANSFER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.add(customerTransferOrderAddOrUp);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改调拨单")
    public ResponseHelper update(@RequestBody CustomerTransferOrderAddOrUp customerTransferOrderAddOrUp, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_FRONT+customerTransferOrderAddOrUp.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.update(customerTransferOrderAddOrUp);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/invoice")
    @ApiOperation("提审或撤回")
    public ResponseHelper invoice( @ApiParam(value = "id") @RequestParam(required = false) String id,
                                   @ApiParam(value = "SAVE保存 WAIT  等待 OK  审核通过") @RequestParam(required = false) String status
                                    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_TRANSFER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.invoice(id,status);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/examine")
    @ApiOperation("审核")
    public ResponseHelper examine( @ApiParam(value = "id") @RequestParam(required = false) String id,
                                   @ApiParam(value = "SAVE保存 WAIT  等待 OK  审核通过") @RequestParam(required = false) String status,
                                   @ApiParam(value = "备注") @RequestParam(required = false) String approvalRemark
    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_TRANSFER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.examine(id,status,approvalRemark);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseHelper delete(@ApiParam(required = false, value = "id") @RequestParam(required = false) String id, 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("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.delete(id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/NeExamine")
    @ApiOperation("反审核")
    public ResponseHelper NeExamine( @ApiParam(value = "id") @RequestParam(required = false) String id,
                                   @ApiParam(value = "SAVE保存 WAIT  等待 OK  审核通过") @RequestParam(required = false) String status,
                                   @ApiParam(value = "备注") @RequestParam(required = false) String approvalRemark
    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_CUSTOMER_TRANSFER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            customerTransferOrderLogic.NeExamine(id,status,approvalRemark);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }




    @ApiOperation(value = "调拨单详情")
    @GetMapping("/detail")
    public ResponseHelper<CustomerTransferOrderAddOrUp> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        CustomerTransferOrderAddOrUp customerStockDetail = customerTransferOrderLogic.detail(id);
        return ResponseHelper.success(customerStockDetail);
    }


}
