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

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.admin.AdminDeptTree;
import com.gree.mall.manager.bean.admin.AdminDeptWebsitVO;
import com.gree.mall.manager.bean.notice.NoticeRecordReadBean;
import com.gree.mall.manager.bean.workorder.IncreAddOrUp;
import com.gree.mall.manager.bean.workorder.IncreVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.admin.AdminDeptLogic;
import com.gree.mall.manager.plus.entity.AdminDept;
import com.gree.mall.manager.plus.entity.AdminDeptWebsit;
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 = "部门级别api", tags = {"部门级别api"})
@RequestMapping(value = "/admin/dept", produces = "application/json; charset=utf-8")
public class AdminDeptController {

    @Autowired
    AdminDeptLogic adminDeptLogic;


    @GetMapping("/tree")
    @ApiOperation("部门树")
    public ResponseHelper<AdminDeptTree> tree(HttpServletRequest request) {
        List<AdminDeptTree> tree = adminDeptLogic.tree(request);
        return ResponseHelper.success(tree);
    }

    @GetMapping("/treeWebsit")
    @ApiOperation("部门树合并网点")
    public ResponseHelper<AdminDeptTree> treeWebsit(HttpServletRequest request) {
        List<AdminDeptTree> tree = adminDeptLogic.treeWebsit(request);
        return ResponseHelper.success(tree);
    }


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "部门网点列表")
    public ResponseHelper<IPage<AdminDeptWebsitVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<AdminDeptWebsitVO> adminDeptVOIPage = adminDeptLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(adminDeptVOIPage, new TypeReference<AdminDeptWebsitVO>() {});
    }

    @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<AdminDeptWebsitVO> adminDeptVOIPage = adminDeptLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(adminDeptVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/add")
    @ApiOperation("添加部门")
    public ResponseHelper add(
            @RequestBody AdminDept adminDept,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.add(adminDept);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改部门")
    public ResponseHelper update(
            @RequestBody AdminDept adminDept,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.update(adminDept);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation("删除部门")
    public ResponseHelper update(
            @ApiParam(value = "部门id" , required = true) @RequestParam(required = true) String adminDeptId,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.del(adminDeptId);
        return ResponseHelper.success();
    }

    @PostMapping("/addWebsit")
    @ApiOperation("添加部门网点")
    public ResponseHelper addWebsit(
            @ApiParam(value = "部门id" , required = true) @RequestParam(required = true) String adminDeptId,
            @ApiParam(value = "网点" , required = true) @RequestParam(required = true) List<String> websitIds,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.addWebsit(adminDeptId,websitIds);
        return ResponseHelper.success();
    }

    @PostMapping("/updateWebsit")
    @ApiOperation("修改部门网点")
    public ResponseHelper updateWebsit(
            @RequestBody AdminDeptWebsit adminDeptWebsit,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.updateWebsit(adminDeptWebsit);
        return ResponseHelper.success();
    }

    @PostMapping("/delWebsit")
    @ApiOperation("删除部门网点")
    public ResponseHelper delWebsit(
            @ApiParam(value = "部门网点id" , required = true) @RequestParam(required = true) String adminDeptWebsitId,
            HttpServletRequest request
    ) throws ParseException {
        adminDeptLogic.delWebsit(adminDeptWebsitId);
        return ResponseHelper.success();
    }



    @PostMapping("/import")
    @ApiOperation("导入部门网点")
    public ResponseHelper importData(
            @RequestPart("file") MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        adminDeptLogic.importData(objects);
        return ResponseHelper.success();
    }


}
