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.WebsitNewInListBean;
import com.gree.mall.manager.bean.material.manage.WebsitPartsNewInVO;
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.WebsitPartsNewInLogic;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
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 = "/websit/parts/new-in", produces = "application/json; charset=utf-8")
public class WebsitPartsNewInController {

    @Resource
    WebsitPartsNewInLogic websitPartsNewInLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<IPage<WebsitPartsNewInVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<WebsitPartsNewInVO> partsBeanIPage = websitPartsNewInLogic.pageList(zfireParamBean);
        return ResponseHelper.success(partsBeanIPage, new TypeReference<WebsitPartsNewInVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPartsNewInVO> baseVOIPage = websitPartsNewInLogic.pageList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

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

    @ApiOperation(value = "新增网点新件入库记录")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "网点新件入库记录") @RequestBody List<WebsitNewInListBean> websitNewInListBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.NEW_IN + "add");
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请稍候再操作...");
        }
        try {
            websitPartsNewInLogic.add(websitNewInListBean);
        } catch(Exception e) {
            log.error("【网点新件入库记录处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

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

        return ResponseHelper.success();
    }

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

        return ResponseHelper.success();
    }


}
