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.CustomerFrontLogic;
import com.zfire.mall.manager.logic.front.CustomerOrderInstallLogic;
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/install", produces = "application/json; charset=utf-8")
public class CustomerFrontInstallController {

    @Autowired
    CustomerOrderInstallLogic customerOrderInstallLogic;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "工单安装详情")
    @GetMapping("/detail")
    public ResponseHelper<CustomerOrderInstallDetail> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String gongDanId
    ) throws Exception {
        CustomerOrderInstallDetail customerOrderInstallDetail = customerOrderInstallLogic.detail(gongDanId);
        return ResponseHelper.success(customerOrderInstallDetail);
    }


    @ZfireList
    @PostMapping("/list")
    @ApiOperation("工单安装条码")
    public ResponseHelper<IPage<CustomerCodeInstallVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CustomerCodeInstallVO> stockBeanIPage = customerOrderInstallLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CustomerCodeInstallVO>() {});
    }

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

}
