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.PayDetail;
import com.gree.mall.manager.bean.material.manage.WebsitPartsCreditBean;
import com.gree.mall.manager.bean.material.manage.WebsitPartsCreditItemVO;
import com.gree.mall.manager.bean.material.manage.WebsitPartsCreditVO;
import com.gree.mall.manager.bean.material.manage.WebsitPartsSalesBean;
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.WebsitPartsCreditLogic;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "网点配件记账管理", tags ={"网点配件记账管理"} )
@RequestMapping(value = "/parts/credit", produces = "application/json; charset=utf-8")
public class WebsitPartsCreditController {

    @Resource
    WebsitPartsCreditLogic websitPartsCreditLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/gather/list")
    @ApiOperation(value = "网点汇总记账-列表")
    public ResponseHelper<IPage<WebsitPartsCreditVO>> gatherPage(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPartsCreditVO> page = websitPartsCreditLogic.gatherPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPartsCreditVO>() {});
    }

    @PostMapping("/gather/list/export")
    @ApiOperation("网点汇总记账-列表导出")
    public void gatherListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPartsCreditVO> baseVOIPage = websitPartsCreditLogic.gatherPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "网点记账-列表")
    public ResponseHelper<IPage<WebsitPartsCreditItemVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPartsCreditItemVO> page = websitPartsCreditLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPartsCreditItemVO>() {});
    }

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

    @PostMapping("/detail")
    @ApiOperation("网点记账-详情")
    public ResponseHelper<WebsitPartsCreditBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {
        WebsitPartsCreditBean bean = websitPartsCreditLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/update")
    @ApiOperation("网点记账-更新备注")
    public ResponseHelper update(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark
    ) throws Exception {
        websitPartsCreditLogic.update(id, remark);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "生成支付二维码")
    @PostMapping("/code")
    public ResponseHelper<PayDetail> generateCode(
            @ApiParam(value = "记账id", required = true) @RequestParam String id,
            @ApiParam(value = "网点支付配置id") @RequestParam(required = false) String payConfigId
    ) throws Exception {
        return ResponseHelper.success(websitPartsCreditLogic.generateCode(id, payConfigId));
    }

    @ApiOperation("现金支付")
    @PostMapping("/change-cash")
    public ResponseHelper<WebsitPartsSalesBean> changeCash(@ApiParam(value = "记账id", required = true) @RequestParam String id) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PARTS_SALES + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("记账记录冻结中");
        }
        try {
            websitPartsCreditLogic.changeCash(id);
            return ResponseHelper.success();
        } catch (Exception e) {
            log.error("【记账记录处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
    }
}
