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.service.IBizDayworkItemService;
import cn.ezhizao.project.business.service.IBizProductionResourceGroupDetailService;
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.ArrayList;
import java.util.List;
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;

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

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

    /**
     * 查询工段资源组明细设备列表
     */
    @Transactional
    @GetMapping("/equipmentInfo")
    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);
            }
        }
        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.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));
    }
}
