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

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.letter.LeDepartmentVO;
import com.gree.mall.manager.bean.letter.LeProjectVO;
import com.gree.mall.manager.bean.letter.LeUserDepartmentVO;
import com.gree.mall.manager.bean.policy.PolicyDetail;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.LetterLogic;
import com.gree.mall.manager.plus.entity.LeConfig;
import com.gree.mall.manager.plus.entity.LeDepartment;
import com.gree.mall.manager.plus.entity.LeProject;
import com.gree.mall.manager.plus.entity.LeUserDepartment;
import com.gree.mall.manager.utils.excel.ExcelUtils;
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.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 = "工作联络函基础", tags = {"工作联络函基础"})
@RequestMapping(value = "/letterManager", produces = "application/json; charset=utf-8")
public class LetterController {

    @Autowired
    private LetterLogic letterLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "科室设置列表")
    public ResponseHelper<IPage<LeDepartmentVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<LeDepartmentVO> policyOrderVOIPage = letterLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<LeDepartmentVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "科室设置导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<LeDepartmentVO> policyOrderVOIPage = letterLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/detail")
    @ApiOperation("科室设置详情")
    public ResponseHelper<LeDepartment> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        LeDepartment leDepartment = letterLogic.detail(id);
        return ResponseHelper.success(leDepartment);
    }

    @PostMapping("/add")
    @ApiOperation("科室设置新增")
    public ResponseHelper add(
            @RequestBody LeDepartment leDepartment,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.add(leDepartment);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("科室设置修改")
    public ResponseHelper update(
            @RequestBody LeDepartment leDepartment,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.update(leDepartment);
        return ResponseHelper.success();
    }


    @PostMapping("/del")
    @ApiOperation("科室设置删除")
    public ResponseHelper del(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.del(id);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("/listUser")
    @ApiOperation(value = "所属科室列表")
    public ResponseHelper<IPage<LeUserDepartmentVO>> listUser(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<LeUserDepartmentVO> policyOrderVOIPage = letterLogic.listUser(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<LeUserDepartmentVO>() {
        });
    }

    @PostMapping("/listUser/export")
    @ApiOperation(value = "所属科室列表导出")
    public void listUserExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<LeUserDepartmentVO> policyOrderVOIPage = letterLogic.listUser(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @PostMapping("/detailUser")
    @ApiOperation("所属科室列表详情")
    public ResponseHelper<LeUserDepartment> detailUser(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        LeUserDepartment leDepartment = letterLogic.detailUser(id);
        return ResponseHelper.success(leDepartment);
    }

    @PostMapping("/addUser")
    @ApiOperation("所属科室列表新增")
    public ResponseHelper addUser(
            @RequestBody LeUserDepartment leUserDepartment,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.addUser(leUserDepartment);
        return ResponseHelper.success();
    }

    @PostMapping("/updateUser")
    @ApiOperation("所属科室列表修改")
    public ResponseHelper updateUser(
            @RequestBody LeUserDepartment leUserDepartment,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.updateUser(leUserDepartment);
        return ResponseHelper.success();
    }


    @PostMapping("/delUser")
    @ApiOperation("所属科室列表置删除")
    public ResponseHelper delUser(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.delUser(id);
        return ResponseHelper.success();
    }


    @PostMapping("/import")
    @ApiOperation(value = "所属科室导入")
    public ResponseHelper importUser(
            @RequestParam MultipartFile file
    ) throws RemoteServiceException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        letterLogic.importUser(objects);
        return ResponseHelper.success();
    }



    @PostMapping("/configDeatil")
    @ApiOperation("考核函时间设置")
    public ResponseHelper<LeConfig> configDeatil(
            HttpServletRequest request
    ) throws ParseException {
        LeConfig leConfig = letterLogic.configDeatil();
        return ResponseHelper.success(leConfig);
    }

    @PostMapping("/addConfig")
    @ApiOperation("考核涵配置修改")
    public ResponseHelper addConfig(
            @RequestBody LeConfig leConfig,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.addConfig(leConfig);
        return ResponseHelper.success();
    }



    @ZfireList
    @PostMapping("/listProject")
    @ApiOperation(value = "考核项目设置")
    public ResponseHelper<IPage<LeProjectVO>> listProject(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<LeProjectVO> policyOrderVOIPage = letterLogic.listProject(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<LeProjectVO>() {
        });
    }

    @PostMapping("/listProject/export")
    @ApiOperation(value = "考核项目设置导出")
    public void listProjectExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<LeProjectVO> policyOrderVOIPage = letterLogic.listProject(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/detailProject")
    @ApiOperation("考核项目详情")
    public ResponseHelper<LeProject> detailProject(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        LeProject leProject = letterLogic.detailProject(id);
        return ResponseHelper.success(leProject);
    }

    @PostMapping("/addProject")
    @ApiOperation("考核项目新增")
    public ResponseHelper addProject(
            @RequestBody LeProject leProject,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.addProject(leProject);
        return ResponseHelper.success();
    }

    @PostMapping("/updateProject")
    @ApiOperation("考核项目修改")
    public ResponseHelper updateProject(
            @RequestBody LeProject leProject,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.updateProject(leProject);
        return ResponseHelper.success();
    }


    @PostMapping("/delProject")
    @ApiOperation("考核项目删除")
    public ResponseHelper delProject(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.delProject(id);
        return ResponseHelper.success();
    }

    @PostMapping("/importProject")
    @ApiOperation(value = "考核项目导入")
    public ResponseHelper importProject(
            @RequestParam MultipartFile file
    ) throws RemoteServiceException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        letterLogic.importProject(objects);
        return ResponseHelper.success();
    }



}
