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

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

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.inspectionChamber.domain.BizWorkshopChamber;
import cn.ezhizao.project.business.inspectionChamber.service.IBizWorkshopChamberService;
import cn.ezhizao.project.business.workshop.domain.BizWorkshop;
import cn.ezhizao.project.business.workshop.service.IBizWorkshopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ezhizao.project.business.inspectionChamber.domain.BizInspectionChamber;
import cn.ezhizao.project.business.inspectionChamber.service.IBizInspectionChamberService;

/**
 * 检测室配置Controller
 *
 * @author ezhizao
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/business/inspectionChamber")
public class BizInspectionChamberController extends BaseController
{
    @Resource
    private IBizInspectionChamberService bizInspectionChamberService;
    @Resource
    private IBizWorkshopChamberService bizWorkshopChamberService;
    @Resource
    private IBizWorkshopService bizWorkshopService;

    /**
     * 查询检测室配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:list')")
    @Log(title = "检测室配置查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizInspectionChamber bizInspectionChamber) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(bizInspectionChamber);
        startPage();
        List<BizInspectionChamber> list = bizInspectionChamberService.getList(bizInspectionChamber);
        List<BizWorkshopChamber> chambers = bizWorkshopChamberService.query().in("chamber_id", list.isEmpty() ? Collections.singletonList(0L) : list.stream().map(BizInspectionChamber::getId).collect(Collectors.toList())).list();
        List<BizWorkshop> workshops = bizWorkshopService.query().in("id", chambers.isEmpty() ? Collections.singletonList(0L) : chambers.stream().map(BizWorkshopChamber::getWorkshopId).collect(Collectors.toList())).list();
        list.forEach(l -> {
            l.setWorkshopList(chambers.stream().filter(v -> v.getChamberId().equals(l.getId())).collect(Collectors.toList()));
            l.setWorkShopIds(l.getWorkshopList().stream().map(BizWorkshopChamber::getWorkshopId).collect(Collectors.toList()));
            l.setWorkShops(workshops.stream().filter(v -> l.getWorkShopIds().contains(v.getId())).map(BizWorkshop::getName).collect(Collectors.joining(",")));
        });
        return getDataTable(list);
    }

    /**
     * 导出检测室配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:export')")
    @Log(title = "检测室配置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizInspectionChamber bizInspectionChamber) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(bizInspectionChamber);
        List<BizInspectionChamber> list = bizInspectionChamberService.getList(bizInspectionChamber);
        ExcelUtil<BizInspectionChamber> util = new ExcelUtil<BizInspectionChamber>(BizInspectionChamber.class);
        util.exportExcel(response, list, "检测室配置数据");
    }

    /**
     * 获取检测室配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:query')")
    @Log(title = "检测室配置查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizInspectionChamberService.getById(id));
    }

    /**
     * 新增检测室配置
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:add')")
    @Log(title = "检测室配置新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizInspectionChamber bizInspectionChamber) throws NoSuchFieldException, IllegalAccessException
    {
        // 判断仪器室名称不能重复
        if (bizInspectionChamberService.query().eq("chamber_name", bizInspectionChamber.getChamberName()).count() > 0) {
            return error("仪器室名称不可重复");
        }
//        setTenantId(bizInspectionChamber);
        return toAjax(bizInspectionChamberService.save(bizInspectionChamber, bizInspectionChamber.getWorkShopIds()));
    }

    /**
     * 修改检测室配置
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:edit')")
    @Log(title = "检测室配置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizInspectionChamber bizInspectionChamber) throws NoSuchFieldException, IllegalAccessException
    {
        // 判断仪器室名称不能重复
        if (bizInspectionChamberService.query().eq("chamber_name", bizInspectionChamber.getChamberName()).ne("id", bizInspectionChamber.getId()).count() > 0) {
            return error("仪器室名称不可重复");
        }
//        setTenantId(bizInspectionChamber);
        return toAjax(bizInspectionChamberService.updateById(bizInspectionChamber, bizInspectionChamber.getWorkShopIds()));
    }

    /**
     * 删除检测室配置
     */
    @PreAuthorize("@ss.hasPermi('business:inspectionChamber:remove')")
    @Log(title = "检测室配置删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizInspectionChamberService.removeBatchByIds(ids));
    }
}
