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

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.worker.WebsitWorkerBean;
import com.gree.mall.manager.bean.worker.WorkerTeamVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.worker.WorkerTeamLogic;
import com.gree.mall.manager.plus.entity.WorkerTeam;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "师傅组别", tags = {"师傅组别"})
@RequestMapping(value = "/worker/team", produces = "application/json; charset=utf-8")
public class WorkerTeamController {

    @Resource
    WorkerTeamLogic workerTeamLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<WorkerTeamVO>> list(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<WorkerTeamVO> list = workerTeamLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<WorkerTeamVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerTeamVO> list = workerTeamLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/add")
    @ApiOperation("新增")
    public ResponseHelper add(@RequestBody WorkerTeam workerTeam) throws RemoteServiceException {
        workerTeamLogic.add(workerTeam);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseHelper update(@RequestBody WorkerTeam workerTeam) throws RemoteServiceException {
        workerTeamLogic.update(workerTeam);
        return ResponseHelper.success();
    }

    @PostMapping("/query/websit/worker")
    @ApiOperation("查询网点入驻的师傅")
    public ResponseHelper<List<WebsitWorkerBean>> queryWebsitWorker (
            @ApiParam(value = "网点编号", required = true) @RequestParam String websitId,
            @ApiParam(value = "已选师傅编号") @RequestParam(required = false) String workerId
    ) {
        List<WebsitWorkerBean> list = workerTeamLogic.queryWebsitWorker(websitId, workerId);
        return ResponseHelper.success(list);
    }

    @PostMapping("/import")
    @ApiOperation(value = "网点管理-导入(下载模板:师傅组队导入模板.xlsx)")
    public ResponseHelper<String> importData(
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        workerTeamLogic.importData(file);
        return ResponseHelper.success();
    }
}
