package com.dayou.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.dto.SimpleDepartmentModel;
import com.dayou.vo.DepartmentStatVO;
import com.dayou.vo.DepartmentVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IDepartmentService;
import com.dayou.entity.Department;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.multipart.MultipartFile;
/**
 * 机构
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("department")
@Slf4j
public class DepartmentController extends BaseController {
    @Autowired
    private IDepartmentService departmentService;

    /**
    * 机构列表
    */
    @GetMapping("")
    public RestResponse<Page<DepartmentVO>> page(Department department, Page page){
        Page<DepartmentVO> pages=departmentService.selectPage(page,department);
        return RestResponse.data(pages);
    }

    /**
     * 机构详情
     */
    @GetMapping("/{id}")
    public RestResponse<Department> detail(@PathVariable Long id){
        Department xDepartment =departmentService.detail(id);
        return RestResponse.data(xDepartment);
     }

    /**
     * 机构新增
     */
    @OperLog(title = "新增部门")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody Department department) {
        Boolean ret = departmentService.add(department);
        return RestResponse.data(ret);
    }

    /**
     * 机构更新
     */
    @OperLog(title = "更新部门")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody Department department) {
        Boolean ret = departmentService.update(department);
        return RestResponse.data(ret);
    }

    /**
     * 机构删除
     */
    @OperLog(title = "删除部门")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = departmentService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 机构下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<Department>()
        .select(Department::getId,Department::getName)
        .eq(Department::getDeleted,false);
        List<Department> list= departmentService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }


    /**
     * 机构下拉列表(根据业务类型获取接单部门)
     */
    @GetMapping("/allot/{businessCode}")
    public RestResponse<List<PullDownModel>> allotDepartment(@PathVariable String businessCode){
//        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<Department>()
//                .select(Department::getId,Department::getName)
//                .eq(Department::getDeleted,false).eq(Department::getIfAllot,true);
//        List<Department> list= departmentService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(departmentService.getAllotDepartmentByBusiness(businessCode), PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 机构模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = departmentService.generateImportTemplate();

            String fileName = "机构导入模板" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("生成机构导入模板异常", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }
    }

    /**
     * 机构导入
     */
    @PostMapping("/importExcel")
    public RestResponse<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        departmentService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 日志总览分页
     * @param department
     * @param page
     * @return
     */
    @DataPermission(department = true,postChild = false)
    @GetMapping("/permission/page")
    public RestResponse<IPage<DepartmentVO>> permissionPage(SimpleDepartmentModel department, Page page){
        IPage<DepartmentVO> result =  departmentService.xPage(department,page);
        return RestResponse.data(result);
    }

    /**
     * 分页
     * @param department
     * @param page
     * @return
     */
    @GetMapping("/page")
    public RestResponse<IPage<DepartmentVO>> xPage(SimpleDepartmentModel department, Page page){
        IPage<DepartmentVO> result =  departmentService.xPage(department,page);
        return RestResponse.data(result);
    }

    /**
     * 统计
     * @return
     */
    @GetMapping("/stat")
    public RestResponse<DepartmentStatVO> stat() {
        DepartmentStatVO bo = departmentService.stat();
        return RestResponse.data(bo);
    }
}

