package com.gree.mall.manager.controller.material.manage;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.material.manage.WebsitPartsOldOutVO;
import com.gree.mall.manager.bean.material.manage.WebsitStockOutListBean;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.manage.WebsitPartsOldOutLogic;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.PartsParamBean;
import com.gree.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.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
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;

@Slf4j
@RestController
@Api(value = "网点旧件出库记录", tags ={"网点旧件出库记录"} )
@RequestMapping(value = "/parts/old-out", produces = "application/json; charset=utf-8")
public class WebsitPartsOldOutController {

    @Resource
    WebsitPartsOldOutLogic websitPartsOldOutLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation("网点旧件出库记录列表")
    public ResponseHelper<IPage<WebsitPartsOldOutVO>> list(
            @RequestBody PartsParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<WebsitPartsOldOutVO> partsBeanIPage = websitPartsOldOutLogic.pageList(zfireParamBean);
        return ResponseHelper.success(partsBeanIPage, new TypeReference<WebsitPartsOldOutVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("网点旧件出库记录列表导出")
    public void listExport(
            @RequestBody PartsParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPartsOldOutVO> stockBeanIPage = websitPartsOldOutLogic.pageList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ApiOperation(value = "新增网点新旧件出库记录")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "网点出库记录") @RequestBody List<WebsitStockOutListBean> beans
    ) throws Exception {
        websitPartsOldOutLogic.add(beans);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "审核网点新旧件出库记录")
    @PostMapping("/confirm")
    public ResponseHelper confirm(
            @ApiParam(required = true, value = "网点出库记录") @RequestBody List<String> ids
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.OLD_OUT + "add");
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请稍候, 正在审核中");
        }
        try {
            websitPartsOldOutLogic.confirm(ids);
        } catch(Exception e) {
            log.error("【网点旧件出库记录处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/import")
    @ApiOperation("导入网点旧件出库记录")
    public ResponseHelper importOldOut(
            MultipartFile file
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.OLD_OUT + "add");
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请稍候再导入...");
        }
        try {
            List<Object> objects = ExcelUtils.importExcel(file);
            websitPartsOldOutLogic.importOldOut(objects);
        } catch(Exception e) {
            log.error("【网点旧件出库记录处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除网点旧件出库记录")
    @PostMapping("/del")
    public ResponseHelper delOldOut(
            @ApiParam(required = true, value = "网点旧件出库记录id") @RequestParam List<String> ids
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.OLD_OUT + "add");
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请稍候再操作...");
        }
        try {
            websitPartsOldOutLogic.delOldOut(ids);
        } catch(Exception e) {
            log.error("【网点旧件出库记录处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

}
