package cn.ezhizao.project.business.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.domain.BizDayworkItem;
import cn.ezhizao.project.business.domain.BizProductionPlanDetail;
import cn.ezhizao.project.business.domain.BizProductionResourceGroupDetail;
import cn.ezhizao.project.business.domain.BizProductionResourceGroupSubPlan;
import cn.ezhizao.project.business.service.IBizDayworkItemService;
import cn.ezhizao.project.business.service.IBizProductionResourceGroupDetailService;
import cn.ezhizao.project.business.service.IBizProductionResourceGroupSubPlanService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

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

    /**
     * 查询工段资源组明细列表
     */
    @GetMapping("/list")
    @Log(title = "查询工段资源组明细列表", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizProductionResourceGroupDetail bizProductionResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException
    {

        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getList(bizProductionResourceGroupDetail);
        return getDataTable(list);
    }

    /**
     * 查询工段资源组明细设备列表
     */
    @Transactional
    @GetMapping("/equipmentInfo")
    @Log(title = "查询工段资源组明细设备列表", businessType = BusinessType.SELECT)
    public TableDataInfo equipmentInfo(BizProductionResourceGroupDetail bizProductionResourceGroupDetail)
    {

        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getEquipmentInfo(bizProductionResourceGroupDetail);
        BizProductionResourceGroupDetail equipmentInfo = bizDayworkItemService.getEquipmentInfo(bizProductionResourceGroupDetail);
        for (BizProductionResourceGroupDetail item : list) {
            if (item.getValue().equals(equipmentInfo.getValue())) {
                return getDataTable(list);
            }
        }
        if(equipmentInfo.getValue() !=0) {
            list.add(equipmentInfo);
        }
//        //查询是否有正在使用的设备，如果有那么过滤掉
//        //先查出自己本身所绑定的设备
//        BizDayworkItem bizDayworkItem = bizDayworkItemService.query().eq("id", bizProductionResourceGroupDetail.getDayworkItemId()).eq("deleted", 0).list().get(0);
//
//        //根据设备筛掉正在使用
//        List<Long> ids = new ArrayList<>();
//        for (BizProductionResourceGroupDetail item:list
//        ) {
//            ids.add(item.getValue());
//        }
//        List<BizDayworkItem> screenList = bizDayworkItemService.query().in("equipment_detail_id", ids).lt("status", 2).list();
//        List<BizProductionResourceGroupDetail> newList = new ArrayList<>();
//        for (BizProductionResourceGroupDetail listItem : list) {
//            if(listItem.getValue().equals(bizDayworkItem.getEquipmentDetailId())) {
//                newList.add(listItem);
//            }
//            boolean shouldRemove = false;
//            for (BizDayworkItem item : screenList) {
//                if (listItem.getValue().equals(item.getEquipmentDetailId())) {
//                    shouldRemove = true;
//                    break;
//                }
//            }
//            if (!shouldRemove) {
//                newList.add(listItem);
//            }
//        }
//        list = newList;
        return getDataTable(list);
    }
    @Log(title = "派工信息选择设备", businessType = BusinessType.SELECT)
    @Transactional
    @GetMapping("/equipmentInfoInDaywork")
    public TableDataInfo equipmentInfoInDaywork(BizProductionResourceGroupDetail bizProductionResourceGroupDetail)
    {
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getEquipmentInfo(bizProductionResourceGroupDetail);
        return getDataTable(list);
    }
    //获得当前工段和人员所在资源组绑定的设备
    @PostMapping("/getEquipmentList")
    @Log(title = "获取当前工段和人员所在资源组绑定的设备", businessType = BusinessType.SELECT)
    public TableDataInfo getEquipmentList(@RequestBody BizProductionResourceGroupDetail bizProductionResourceGroupDetail) {
        List<BizProductionResourceGroupDetail> groupDetailList = bizProductionResourceGroupDetailService.query().eq("common_type", "person")
                .eq("common_id", bizProductionResourceGroupDetail.getCommonId())
                .eq("dept_id", bizProductionResourceGroupDetail.getDeptId()).list();
        List<Long> productionResourceGroupIds = groupDetailList.stream().map(BizProductionResourceGroupDetail::getProductionResourceGroupId).distinct().collect(Collectors.toList());
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.query()
                .eq("common_type", "equipmentDetail")
                .in("production_resource_group_id", productionResourceGroupIds.isEmpty() ? Collections.singletonList(0L) : productionResourceGroupIds).list();
        return getDataTable(list);
    }
    @GetMapping ("/getUserInfoByDeptIdAndProductionPlanDetailId")
    public TableDataInfo getUserInfoByDeptIdAndProductionPlanDetailId(BizProductionResourceGroupSubPlan bizProductionResourceGroupSubPlan) {
        startPage();
        List<BizProductionResourceGroupDetail> list = bizProductionResourceGroupDetailService.getListByResourceGroupIds(bizProductionResourceGroupSubPlan);
        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, "工段资源组明细数据");
    }

    /**
     * 获取工段资源组明细详细信息
     */
    @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));
    }
}
