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

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.resourceGroup.domain.BizProductionResourceGroup;
import cn.ezhizao.project.business.resourceGroup.domain.BizProductionResourceGroupDetail;
import cn.ezhizao.project.business.resourceGroup.service.IBizProductionResourceGroupDetailService;
import cn.ezhizao.project.system.domain.SysDeptUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 工段资源组明细Controller
 *
 * @author ezhizao
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/business/detail")
public class BizProductionResourceGroupDetailController extends BaseController {
    @Resource
    private IBizProductionResourceGroupDetailService bizProductionResourceGroupDetailService;

    /**
     * 查询工段资源组明细列表
     */
    @Log(title = "工段资源组明细查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException {
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getList(bizProductionResourceGroupDetail);
        return getDataTable(list);
    }

    /**
     * 查询工段资源组明细列表
     */
    @Log(title = "工段资源组明细查询设备", businessType = BusinessType.SELECT)
    @GetMapping("/equipmentList")
    public TableDataInfo EquipmentList(BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException {
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getEquipmentList(bizProductionResourceGroupDetail);
        return getDataTable(list);
    }

    /**
     * 导出工段资源组明细列表
     */
    @Log(title = "工段资源组明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException {
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getList(bizProductionResourceGroupDetail);
        ExcelUtil<BizProductionResourceGroupDetail> util = new ExcelUtil<BizProductionResourceGroupDetail>(BizProductionResourceGroupDetail.class);
        util.exportExcel(response, list, "工段资源组明细数据");
    }

    /**
     * 获取工段资源组明细详细信息
     */
    @Log(title = "工段资源组明细查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProductionResourceGroupDetailService.getById(id));
    }

    /**
     * 新增工段资源组明细
     */
    @Log(title = "工段资源组明细新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException {

        return toAjax(bizProductionResourceGroupDetailService.save(bizProductionResourceGroupDetail));
    }

    /**
     * 修改工段资源组明细
     */
    @Log(title = "工段资源组明细修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException {

        return toAjax(bizProductionResourceGroupDetailService.updateById(bizProductionResourceGroupDetail));
    }

    /**
     * 删除工段资源组明细
     */
    @Log(title = "工段资源组明细删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        return toAjax(bizProductionResourceGroupDetailService.removeById(ids));
    }

    /**
     * 删除工段资源组(当删除部门人员时，执行删除操作）
     */
    @Log(title = "工段删除人员，删除资源组明细当前的人员", businessType = BusinessType.DELETE)
    @PostMapping("/deleteGroupDetail")
    public AjaxResult deleteGroupDetail(@RequestBody List<SysDeptUser> deptUserList) {
        QueryWrapper<BizProductionResourceGroupDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptUserList.get(0).getDeptId());
        queryWrapper.and(wrapper -> {
            for (SysDeptUser deptUser : deptUserList) {
                wrapper.or().eq("common_id", deptUser.getUserId());
            }

        });
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.list(queryWrapper);
        if(list.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (BizProductionResourceGroupDetail bizProductionResourceGroupDetail : list) {
                ids.add(bizProductionResourceGroupDetail.getId());
            }
            return toAjax(bizProductionResourceGroupDetailService.removeBatchByIds(ids));
            }
        return success();
    }
}