package com.gree.mall.manager.controller.websit;

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.websit.WebsitSalesRetAddUP;
import com.gree.mall.manager.bean.websit.WebsitSalesRetDetail;
import com.gree.mall.manager.bean.websit.WebsitSalesRetVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.websit.WebsitSalesRetLogic;
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.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "辅材销售退货单", tags = {"辅材销售退货单"})
@RequestMapping(value = "/websitSalesRet", produces = "application/json; charset=utf-8")
public class WebsitSalesRetController {

    @Autowired
    private WebsitSalesRetLogic websitSalesRetLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "辅材销售退货单列表")
    public ResponseHelper<IPage<WebsitSalesRetVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitSalesRetVO> page = websitSalesRetLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitSalesRetVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "辅材销售退货单列表导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WebsitSalesRetVO> page = websitSalesRetLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/add")
    @ApiOperation("创建退货单")
    public ResponseHelper save(
            @RequestBody WebsitSalesRetAddUP websitSalesRetAddUP,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.MATERIAL_SALES + websitSalesRetAddUP.getSalesId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                websitSalesRetLogic.save(websitSalesRetAddUP);
            }
        } catch (Exception e) {
            log.error("【辅材退货订单处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改退货单")
    public ResponseHelper update(
            @RequestBody WebsitSalesRetAddUP websitSalesRetAddUP,
            HttpServletRequest request
    ) throws Exception {
        websitSalesRetLogic.update(websitSalesRetAddUP);
        return ResponseHelper.success();
    }


    @PostMapping("/del")
    @ApiOperation("删除退货单")
    public ResponseHelper del(
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) List<String> salesRetId,
            HttpServletRequest request
    ) throws ParseException {
        websitSalesRetLogic.del(salesRetId);
        return ResponseHelper.success();
    }


    @PostMapping("/detail")
    @ApiOperation("退货单详情")
    public ResponseHelper<WebsitSalesRetDetail> detail(
            @ApiParam(value = "退货单id" , required = true) @RequestParam(required = true) String id,

            HttpServletRequest request
    ) throws ParseException {
        WebsitSalesRetDetail  websitSalesRetDetail = websitSalesRetLogic.detail(id);
        return ResponseHelper.success(websitSalesRetDetail);
    }

}
