package cn.ezhizao.project.business.workOrder.controller;

import cn.ezhizao.common.exception.ServiceException;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.company.domain.BizCompany;
import cn.ezhizao.project.business.company.service.IBizCompanyService;
import cn.ezhizao.project.business.entrust.domain.BizEntrust;
import cn.ezhizao.project.business.entrust.service.IBizEntrustService;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrder;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrderExport;
import cn.ezhizao.project.business.workOrder.domain.vo.EntrustVo;
import cn.ezhizao.project.business.workOrder.service.IBizWorkOrderService;
import cn.ezhizao.project.system.domain.SysCurrentMonth;
import cn.ezhizao.project.system.service.ISysCurrentMonthService;
import cn.ezhizao.project.tenant.domain.Tenant;
import cn.ezhizao.project.tenant.service.ITenantService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 本月委托工单Controller
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/business/workOrderCurrent")
public class WorkOrderCurrentController extends BaseController {
    @Resource
    IBizWorkOrderService bizWorkOrderService;
    @Resource
    ISysCurrentMonthService sysCurrentMonthService;
    @Resource
    IBizEntrustService bizEntrustService;
    @Resource
    ITenantService tenantService;
    @Resource
    IBizCompanyService bizCompanyService;

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizWorkOrder);
        startPage();
        startOrderBy();
//        bizWorkOrder.setIsStop(0);
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).eq("type", 0).list();
        if (months.isEmpty()) {
            return getDataTable(new ArrayList<>());
        } else {
            BizEntrust bizEntrust = new BizEntrust();
            bizEntrust.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
            bizEntrust.setWorkMonth(DateTime.of(months.get(0).getYear() + "-" + months.get(0).getMonth() + "-01", "yyyy-MM-dd").toSqlDate());
            List<BizEntrust> entrusts = bizEntrustService.getList(bizEntrust);
//            bizEntrustService.query().in("work_order_id", list.stream().map(BizWorkOrder::getId).collect(Collectors.toList())).eq("work_month", months.get(0).getYear() + "-" + months.get(0).getMonth() + "-01").list();
            list.forEach(l -> {
                l.setEntrust(entrusts.stream().filter(v -> v.getWorkOrderId().equals(l.getId())).findFirst().orElse(null));
            });
            return getDataTable(list);
        }
    }

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:list')")
    @GetMapping("/listForFactory")
    public TableDataInfo listForFactory(BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException {
        bizWorkOrder.setToTenantId(getTenantId());
        startPage();
//        bizWorkOrder.setIsStop(0);
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).eq("type", 0).list();
        if (months.isEmpty()) {
            return getDataTable(new ArrayList<>());
        } else {
            BizEntrust bizEntrust = new BizEntrust();
            bizEntrust.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
            bizEntrust.setWorkMonth(DateTime.of(months.get(0).getYear() + "-" + months.get(0).getMonth() + "-01", "yyyy-MM-dd").toSqlDate());
            List<BizEntrust> entrusts = bizEntrustService.getList(bizEntrust);
//            bizEntrustService.query().in("work_order_id", list.stream().map(BizWorkOrder::getId).collect(Collectors.toList())).eq("work_month", months.get(0).getYear() + "-" + months.get(0).getMonth() + "-01").list();
            list.forEach(l -> {
                l.setEntrust(entrusts.stream().filter(v -> v.getWorkOrderId().equals(l.getId())).findFirst().orElse(null));
            });
            return getDataTable(list);
        }
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:list')")
    @GetMapping("/checkCurrent")
    public AjaxResult checkCurrent() {
        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).eq("type", 0).list();
        Map<String, Object> result = new HashMap<>();
        if (months.isEmpty()) {
            result.put("checkResult", false);
            return success(result);
        } else {
            result.put("checkResult", true);
            DateTime dateTime = new DateTime();
            dateTime.setField(DateField.YEAR, Integer.parseInt(months.get(0).getYear()));
            dateTime.setField(DateField.DAY_OF_MONTH, 1);
            dateTime.setField(DateField.MONTH, Integer.parseInt(months.get(0).getMonth()) - 1);
            result.put("currentMonth", dateTime.toString("yyyy-MM-dd"));
            return success(result);
        }
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:initCurrent')")
    @GetMapping("/initCurrent")
    public AjaxResult initCurrent(@RequestParam String year, @RequestParam String month) throws NoSuchFieldException, IllegalAccessException {
        // 刚开始建立一个租户后，初始化当期
        SysCurrentMonth sysCurrentMonth = new SysCurrentMonth();
        setTenantId(sysCurrentMonth);
        sysCurrentMonth.setMonth(month);
        sysCurrentMonth.setYear(year);
        sysCurrentMonth.setType(0);

        DateTime dateTime = new DateTime(year + month + "01", "yyyyMMdd");
        dateTime.setField(DateField.MONTH, dateTime.getField(DateField.MONTH) + 1);
        SysCurrentMonth nextCurrentMonth = new SysCurrentMonth();
        setTenantId(nextCurrentMonth);
        nextCurrentMonth.setMonth(dateTime.toString("MM"));
        nextCurrentMonth.setYear(dateTime.toString("yyyy"));
        nextCurrentMonth.setType(1);

        boolean states = sysCurrentMonthService.save(sysCurrentMonth);
        states = states && sysCurrentMonthService.save(nextCurrentMonth);
        return toAjax(states);
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:edit')")
    @PostMapping("/addEntrust")
    public AjaxResult addEntrust(@RequestBody BizEntrust entrust) {
        // 添加本月
        boolean status = addEntrust(entrust.getWorkOrderIds(), entrust.getWorkMonth(), entrust.getToTenantId());
        // 添加下一月
        DateTime currentMonth = DateTime.of(entrust.getWorkMonth());
        status = status && addEntrust(entrust.getWorkOrderIds(), currentMonth.offset(DateField.MONTH, 1).toSqlDate(), entrust.getToTenantId());
        // 添加委托
        return toAjax(status);
    }

    @PostMapping("/importEntrust")
    @Transactional
    public AjaxResult importEntrust(MultipartFile file) throws Exception {
        AtomicInteger failureNum = new AtomicInteger();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        try{

            Long tenantId = getTenantId();
            //获取当前月
            SysCurrentMonth sysCurrentMonth = sysCurrentMonthService.getOne(new QueryWrapper<SysCurrentMonth>().eq("tenant_id", tenantId).eq("type", 0));
            String day = "-01";
            String workMonth = sysCurrentMonth.getYear() + "-" + sysCurrentMonth.getMonth() + day;
            ExcelUtil<EntrustVo> util = new ExcelUtil<>(EntrustVo.class);
            List<EntrustVo> entrustVos = util.importExcel(file.getInputStream());
            List<EntrustVo> newEntrustList = new ArrayList<>();
            entrustVos.forEach(f -> {
                ArrayList<Integer> objects = new ArrayList<>();
                if (f.getTaskName1().equals("是")) {
                    objects.add(1);
                }
                if (f.getTaskName2().equals("是")) {
                    objects.add(2);
                }
                if (f.getTaskName3().equals("是")) {
                    objects.add(3);
                }
                f.setCount(objects);
            });

            for (EntrustVo entrustVo : entrustVos) {
                for (Integer integer : entrustVo.getCount()) {
                    EntrustVo entrustVo1 = new EntrustVo();
                    if (integer == 1) {
                        entrustVo1.setTaskId(1);
                    } else if (integer == 2) {
                        entrustVo1.setTaskId(2);
                    } else {
                        entrustVo1.setTaskId(3);
                    }
                    entrustVo1.setNo(entrustVo.getNo());
                    entrustVo1.setToEntrustName(entrustVo.getToEntrustName());
                    entrustVo1.setCompanyName(entrustVo.getCompanyName());
                    newEntrustList.add(entrustVo1);
                }
            }

            newEntrustList.forEach(f -> {
                try {
                    BizCompany company = bizCompanyService.getOne(new LambdaQueryWrapper<BizCompany>()
                            .eq(BizCompany::getTenantId, tenantId)
                            .eq(BizCompany::getName, f.getCompanyName().trim()));
                    if (company == null) {
                        failureNum.getAndIncrement();
                        failureMsg.append("<br/>" +"序号为"+f.getNo() +"，" + "客户 " +f.getCompanyName() + "不存在");
                    } else {
                        f.setCompanyId(company.getId());
                    }

                } catch (Exception e) {
                    System.out.println(e);
                }

            });
            //获取工单
            for (EntrustVo e : newEntrustList) {
                List<BizWorkOrder> bizWorkOrders = bizWorkOrderService.list(new LambdaQueryWrapper<BizWorkOrder>()
                        .eq(BizWorkOrder::getTenantId, tenantId)
                        .eq(BizWorkOrder::getIsStop, 0)
                        .eq(BizWorkOrder::getDeleted, 0)
                        .eq(BizWorkOrder::getType, 1)
                        .eq(BizWorkOrder::getTaskTypeId, e.getTaskId())
                        .eq(BizWorkOrder::getCompanyId, e.getCompanyId()));
                if (bizWorkOrders== null ||bizWorkOrders.size() == 0) {
                    failureNum.getAndIncrement();
                    String s = "";
                    if(e.getTaskId() == 1){
                        s = "代理记账服务";
                    } else if (e.getTaskId()==2){
                        s = "社保代缴服务";
                    }else {
                        s = "公积金代缴服务";
                    }
                    failureMsg.append("<br/>" +"序号为"+e.getNo() +"，" + "客户 " + e.getCompanyName() + " 没有"+s);
                }else {
                    e.setWorkOrderIds(bizWorkOrders.stream().map(m -> m.getId()).collect(Collectors.toList()));
                }
            }

            //根据受委托方分组
            Map<String, List<EntrustVo>> map = newEntrustList.stream().collect(Collectors.groupingBy(e -> e.getToEntrustName()));
            for (String s : map.keySet()) {
                BizEntrust bizEntrust = new BizEntrust();
                Tenant one = tenantService.getOne(new LambdaQueryWrapper<Tenant>().eq(Tenant::getAccountName, s));
                bizEntrust.setToTenantId(one.getId());
                bizEntrust.setWorkMonth(Date.valueOf(workMonth));
                List<Long> workOrderIds = map.get(s).stream()
                        .flatMap(e -> e.getWorkOrderIds().stream())
                        .collect(Collectors.toList());
                bizEntrust.setWorkOrderIds(workOrderIds);
                this.addEntrust(bizEntrust);
            }
        }catch (Exception e){

        }


        if (Integer.valueOf(failureNum.toString()) > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功");
        }
        return success(successMsg);
    }

    private boolean addEntrust(List<Long> workOrderId, Date paramWorkMonth, Long toTenantId) {
        BizEntrust entrust = new BizEntrust();
        entrust.setWorkOrderIds(workOrderId);
        entrust.setWorkMonth(paramWorkMonth);
        entrust.setToTenantId(toTenantId);
        // 获取所有已有委托
        List<BizEntrust> entrustList = bizEntrustService.getList(entrust);
        // 获取未有委托的工单
        List<BizWorkOrder> bizWorkOrders = new ArrayList<>();
        if (entrust.getWorkOrderIds().stream().anyMatch(e -> entrustList.stream().noneMatch(t -> t.getWorkOrderId().equals(e)))) {
            bizWorkOrders.addAll(bizWorkOrderService.query().in("id", entrust.getWorkOrderIds().stream().filter(e -> entrustList.stream().noneMatch(t -> t.getWorkOrderId().equals(e))).collect(Collectors.toList())).list());
        }
        // 修改已有委托的委托信息
        entrustList.forEach(l -> {
            l.setToTenantId(entrust.getToTenantId());
        });
        // 添加新委托
        List<BizEntrust> addEntrusts = new ArrayList<>();
        bizWorkOrders.forEach(l -> {
            BizEntrust bizEntrust = new BizEntrust();
            bizEntrust.setWorkOrderId(l.getId());
            bizEntrust.setCompanyId(l.getCompanyId());
            bizEntrust.setToTenantId(entrust.getToTenantId());
            bizEntrust.setTenantId(l.getTenantId());
            bizEntrust.setFromTenantId(l.getTenantId());
            if (l.getTaskTypeId() == 1L) {
                DateTime workMonth = DateTime.of(entrust.getWorkMonth());
                bizEntrust.setCurrentMonth(workMonth.offsetNew(DateField.MONTH, -1).toSqlDate());
            } else {
                bizEntrust.setCurrentMonth(entrust.getWorkMonth());
            }
            bizEntrust.setWorkMonth(entrust.getWorkMonth());
            bizEntrust.setTaskTypeId(l.getTaskTypeId());
            bizEntrust.setTaskTypeDetailId(l.getTaskTypeDetailId());
            addEntrusts.add(bizEntrust);
        });
        // 保存
        return (entrustList.isEmpty() || bizEntrustService.updateBatchById(entrustList)) && (addEntrusts.isEmpty() || bizEntrustService.saveBatch(addEntrusts));
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:edit')")
    @PostMapping("/delEntrust")
    public AjaxResult delEntrust(@RequestBody BizEntrust bizEntrust) {
        List<BizEntrust> entrustList = bizEntrustService.getList(bizEntrust);
        DateTime currentMonth = DateTime.of(bizEntrust.getWorkMonth());
        bizEntrust.setWorkMonth(currentMonth.offset(DateField.MONTH, 1).toSqlDate());
        List<BizEntrust> nextEntrustList = bizEntrustService.getList(bizEntrust);

        boolean status = (entrustList.isEmpty() || bizEntrustService.removeByIds(entrustList.stream().map(BizEntrust::getId).collect(Collectors.toList()))) && (nextEntrustList.isEmpty() || bizEntrustService.removeByIds(nextEntrustList.stream().map(BizEntrust::getId).collect(Collectors.toList())));
        // 取消委托
        return toAjax(status);
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:trans')")
    @GetMapping("/transNext")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult transNext() {
        // 将本月改为上一月，将下月改成本月，添加新月
        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).ge("type", 0).list();
        SysCurrentMonth currentMonth = months.stream().filter(v -> v.getType().equals(0)).findFirst().orElse(new SysCurrentMonth());
        if (currentMonth.getId() == null) {
            throw new ServiceException("未设定本月");
        }
        SysCurrentMonth nextMonth = months.stream().filter(v -> v.getType().equals(1)).findFirst().orElse(new SysCurrentMonth());
        SysCurrentMonth newNextMonth = new SysCurrentMonth();
        currentMonth.setType(-1);
        nextMonth.setType(0);
        DateTime nextMonthDate = DateTime.of(nextMonth.getYear() + nextMonth.getMonth() + "01", "yyyyMMdd");
        newNextMonth.setMonth(nextMonthDate.offsetNew(DateField.MONTH, 1).toString("MM"));
        newNextMonth.setYear(nextMonthDate.offsetNew(DateField.MONTH, 1).toString("yyyy"));
        newNextMonth.setTenantId(getTenantId());
        newNextMonth.setType(1);

        // 获取上月的委托
        List<BizEntrust> entrusts = bizEntrustService.query().eq("tenant_id", getTenantId()).eq("work_month", nextMonthDate.toSqlDate()).list();
        List<BizEntrust> entr = entrusts;
        //将到截至月的委托去掉
        for (BizEntrust b : entr) {
            List<BizWorkOrder> workOrder = bizWorkOrderService.query().eq("id", b.getWorkOrderId()).eq("is_stop", 0).list();
            Calendar calendar = Calendar.getInstance();
            // 设置年份
            calendar.set(Calendar.YEAR, Integer.parseInt(months.get(0).getYear()));
            // 设置月份为（注意：月份是从0开始的，所以要减1）
            calendar.set(Calendar.MONTH, Integer.parseInt(months.get(0).getMonth()) - 1);
            // 设置日期
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            Date currentMonthDate = new java.sql.Date(calendar.getTime().getTime());
            if (!workOrder.isEmpty() && currentMonthDate == workOrder.get(0).getEndMonth()) {
                entrusts.removeIf(s -> s.getWorkOrderId().equals(b.getWorkOrderId()));
            }
        }
        entrusts.forEach(l -> {
            DateTime currentWorkMonth = DateTime.of(l.getWorkMonth());
            DateTime currentMonthDate = DateTime.of(l.getCurrentMonth());
            l.setWorkMonth(currentWorkMonth.offsetNew(DateField.MONTH, 1).toSqlDate());
            l.setCurrentMonth(currentMonthDate.offsetNew(DateField.MONTH, 1).toSqlDate());
            l.setId(null);
        });

        return toAjax(
                (currentMonth.getId() == null || sysCurrentMonthService.updateById(currentMonth)) &&
                        (nextMonth.getId() == null || sysCurrentMonthService.updateById(nextMonth)) &&
                        sysCurrentMonthService.save(newNextMonth) && (entrusts.isEmpty() || bizEntrustService.saveBatch(entrusts))
        );
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:trans')")
    @GetMapping("/transNextForFactory")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult transNextForFactory() {
        // 将本月改为上一月，将下月改成本月，添加新月
        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).ge("type", 0).list();
        SysCurrentMonth currentMonth = months.stream().filter(v -> v.getType().equals(0)).findFirst().orElse(new SysCurrentMonth());
        if (currentMonth.getId() == null) {
            throw new ServiceException("未设定本月");
        }
        SysCurrentMonth nextMonth = months.stream().filter(v -> v.getType().equals(1)).findFirst().orElse(new SysCurrentMonth());
        SysCurrentMonth newNextMonth = new SysCurrentMonth();
        currentMonth.setType(-1);
        nextMonth.setType(0);
        DateTime nextMonthDate = DateTime.of(nextMonth.getYear() + nextMonth.getMonth() + "01", "yyyyMMdd");
        newNextMonth.setMonth(nextMonthDate.offsetNew(DateField.MONTH, 1).toString("MM"));
        newNextMonth.setYear(nextMonthDate.offsetNew(DateField.MONTH, 1).toString("yyyy"));
        newNextMonth.setTenantId(getTenantId());
        newNextMonth.setType(1);

        return toAjax(
                (currentMonth.getId() == null || sysCurrentMonthService.updateById(currentMonth)) &&
                        (nextMonth.getId() == null || sysCurrentMonthService.updateById(nextMonth)) &&
                        sysCurrentMonthService.save(newNextMonth));
    }

    /**
     * 导出本月工单
     */
    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:export')")
    @Log(title = "本月工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException {
        if (bizWorkOrder.getToTenantId() == null) {
            setTenantId(bizWorkOrder);
        }
//        startPage();
        startOrderBy();
//        bizWorkOrder.setIsStop(0);
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        List<BizWorkOrderExport> listExport = new ArrayList<>();

        List<SysCurrentMonth> months = sysCurrentMonthService.query().eq("tenant_id", getTenantId()).eq("type", 0).list();
        if (months.isEmpty()) {
            list = new ArrayList<>();
            if (bizWorkOrder.getTenantId() == null) {
                ExcelUtil<BizWorkOrderExport> util = new ExcelUtil<BizWorkOrderExport>(BizWorkOrderExport.class);
                util.exportExcel(response, listExport, "本月工单");
            } else {
                ExcelUtil<BizWorkOrder> util = new ExcelUtil<BizWorkOrder>(BizWorkOrder.class);
                util.exportExcel(response, list, "本月工单");
            }

        } else {
            BizEntrust bizEntrust = new BizEntrust();
            bizEntrust.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
            bizEntrust.setWorkMonth(DateTime.of(months.get(0).getYear() + "-" + months.get(0).getMonth() + "-01", "yyyy-MM-dd").toSqlDate());
            List<BizEntrust> entrusts = bizEntrustService.getList(bizEntrust);
            list.forEach(l -> {
                l.setEntrust(entrusts.stream().filter(v -> v.getWorkOrderId().equals(l.getId())).findFirst().orElse(null));
                l.setTypeName(l.getType() == 1 ? "循环工单" : "代办工单");
                l.setExecutor(l.getEntrust() == null ? l.getServiceName() : l.getEntrust().getToAccountName());
                if (bizWorkOrder.getTenantId() == null) {
                    BizWorkOrderExport export = new BizWorkOrderExport();
                    export.setCompanyName(l.getCompanyName());
                    export.setFromCompanyName(l.getFromCompanyName());
                    export.setSocialCreditCode(l.getSocialCreditCode());
                    export.setTypeName(l.getTypeName());
                    export.setTaskTypeName(l.getTaskTypeName());
                    export.setStartMonth(l.getStartMonth());
                    export.setEndMonth(l.getEndMonth());
                    export.setMonthNum(l.getMonthNum());
                    export.setExecutor(l.getExecutor());
                    listExport.add(export);
                }
            });

            if (bizWorkOrder.getTenantId() == null) {
                ExcelUtil<BizWorkOrderExport> util = new ExcelUtil<BizWorkOrderExport>(BizWorkOrderExport.class);
                util.exportExcel(response, listExport, "本月工单");
            } else {
                ExcelUtil<BizWorkOrder> util = new ExcelUtil<BizWorkOrder>(BizWorkOrder.class);
                util.exportExcel(response, list, "本月工单");
            }
        }
    }

    @PreAuthorize("@ss.hasPermi('business:entrust:workOrder:current:edit')")
    @GetMapping("/getFactories")
    public AjaxResult getFactories() {

        List<Tenant> tenants = tenantService.getFactoriesId(getTenantId());
        return success(tenants);
    }
}
