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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.listvo.User2VO;
import com.gree.mall.manager.bean.listvo.UserCustomerVO;
import com.gree.mall.manager.bean.listvo.UserVO;
import com.gree.mall.manager.bean.listvo.workorder.UserWaitVO;
import com.gree.mall.manager.bean.member.UserApplyBean;
import com.gree.mall.manager.bean.policy.PolicyDetail;
import com.gree.mall.manager.bean.policy.WorkerManagerBean;
import com.gree.mall.manager.bean.policy.WorkerManagerDetail;
import com.gree.mall.manager.bean.user.UserApplyWorkerBean;
import com.gree.mall.manager.bean.user.UserWxBean;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsLogic;
import com.gree.mall.manager.logic.policy.WorkerManagerLogic;
import com.gree.mall.manager.logic.user.UserLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.utils.CommonUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.WorkerParamBean;
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 me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
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.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "师傅管理API", tags = {"师傅管理API"})
@RequestMapping(value = "/workerManager", produces = "application/json; charset=utf-8")
public class WorkerManagerController {

    @Autowired
    UserLogic userLogic;

    @Autowired
    WorkerManagerLogic workerManagerLogic;


    @ZfireList
    @PostMapping("/list/page2")
    @ApiOperation(value = "师傅列表-v2")
    public ResponseHelper<Page<UserVO>> page1(
            @RequestBody WorkerParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<UserVO> page = userLogic.page2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<UserVO>() {
        });
    }

    @PostMapping("/list/page2/export")
    @ApiOperation(value = "师傅列表-导出-v2")
    public void page1Export(
            @RequestBody WorkerParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<UserVO> page = userLogic.page2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }


    @PostMapping("/stop")
    @ApiOperation("暂停接单")
    public ResponseHelper stop(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.stop(id);
        return ResponseHelper.success();
    }


    @PostMapping("/activation")
    @ApiOperation("激活账号")
    public ResponseHelper activation(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.activation(id);
        return ResponseHelper.success();
    }


    @PostMapping("/quit")
    @ApiOperation("离职")
    public ResponseHelper quit(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.quit(id);
        return ResponseHelper.success();
    }


    @PostMapping("/againQuit")
    @ApiOperation("重新入职")
    public ResponseHelper againQuit(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.againQuit(id);
        return ResponseHelper.success();
    }


    @PostMapping("/update")
    @ApiOperation("编辑师傅")
    public ResponseHelper update(
            @RequestBody WorkerManagerBean workerManagerBean,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.update(workerManagerBean);
        return ResponseHelper.success();
    }


    @PostMapping("/detail")
    @ApiOperation("师傅详情")
    public ResponseHelper<WorkerManagerDetail> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        WorkerManagerDetail workerManagerDetail = workerManagerLogic.detail(id);
        return ResponseHelper.success(workerManagerDetail);
    }


    @PostMapping("/examineWait")
    @ApiOperation("入驻审批")
    public ResponseHelper examineWait(
            @RequestBody WorkerManagerBean workerManagerBean,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.examineWait(workerManagerBean);
        return ResponseHelper.success();
    }


    @PostMapping("/examine")
    @ApiOperation("保险审批")
    public ResponseHelper examine(
            @RequestBody WorkerManagerBean workerManagerBean,
            HttpServletRequest request
    ) throws ParseException {
        workerManagerLogic.examine(workerManagerBean);
        return ResponseHelper.success();
    }


}
