package com.zfire.mall.manager.controller.customer;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.ExcelData;
import com.zfire.mall.manager.bean.front.CustomerOrderPushBean;
import com.zfire.mall.manager.bean.front.CustomerPushBean;
import com.zfire.mall.manager.bean.k3.K3CustomerBean;
import com.zfire.mall.manager.bean.listV2.K3CustomerVO;
import com.zfire.mall.manager.bean.listV2.StageK3CustomerVO;
import com.zfire.mall.manager.bean.pick.PickManagerDetailBean;
import com.zfire.mall.manager.bean.pick.PickManagerRequest;
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.customer.Stagek3CustomerLogic;
import com.zfire.mall.manager.logic.k3.K3CustomerLogic;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
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.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "子商户API", tags ={"子商户API"} )
@RequestMapping(value = "/StageCustomer", produces = "application/json; charset=utf-8")
public class StageCustomerController {

    @Autowired
    Stagek3CustomerLogic stagek3CustomerLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ZfireList
    @PostMapping("/list/v3")
    @ApiOperation(value = "全部经销商列表V3")
    public ResponseHelper<K3CustomerVO> listV3(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<K3CustomerVO> k3CustomerVOIPage = stagek3CustomerLogic.listV3(zfireParamBean);
        return ResponseHelper.success(k3CustomerVOIPage, new TypeReference<K3CustomerVO>() {});
    }



    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "子经销商列表V2")
    public ResponseHelper<StageK3CustomerVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<StageK3CustomerVO> k3CustomerVOIPage = stagek3CustomerLogic.listV2(zfireParamBean);
        return ResponseHelper.success(k3CustomerVOIPage, new TypeReference<StageK3CustomerVO>() {});
    }

    @PostMapping("/user/export/v2")
    @ApiOperation("子经销商列表V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StageK3CustomerVO> stockBeanIPage = stagek3CustomerLogic.listV2(zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @PostMapping("/add")
    @ApiOperation("子经销商-新增")
    public ResponseHelper addStock(@RequestBody KingDeeCustomer kingDeeCustomer, HttpServletRequest request) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stagek3CustomerLogic.add(kingDeeCustomer, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/update")
    @ApiOperation("修改子经销商")
    public ResponseHelper update(@RequestBody KingDeeCustomer kingDeeCustomer, HttpServletRequest request) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER + kingDeeCustomer.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stagek3CustomerLogic.update(kingDeeCustomer, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "子经销商详情")
    @GetMapping("/detail")
    public ResponseHelper<KingDeeCustomer> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        KingDeeCustomer kingDeeCustomer = stagek3CustomerLogic.detail(id);
        return ResponseHelper.success(kingDeeCustomer);
    }


    @PostMapping("/examine")
    @ApiOperation("子经销商审核")
    public ResponseHelper update( @ApiParam(required = true, value = "id") @RequestParam(required = true) String id,
                                  @ApiParam(required = true, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = true) String examineStatus,
                                  @ApiParam(required = false, value = "审批备注") @RequestParam(required = false) String remark,
                                 HttpServletRequest request) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stagek3CustomerLogic.examine(id,examineStatus,remark, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @ApiOperation(value = "同步经销商")
    @PostMapping("/customerPush")
    @ApiNotAuth
    @Sign
    public ResponseHelper<String> customerOrderPush(
            @ApiParam(required = false, value = "更新时间,秒级别") @RequestParam(required = false) String encrypted
    ) throws Exception {
        List<CustomerPushBean> customerOrderBeanIPage = stagek3CustomerLogic.listPush(encrypted);
        String pi = JSONUtil.toJsonStr(customerOrderBeanIPage);
        return ResponseHelper.success(pi);
    }


}
