package cn.ezhizao.project.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.domain.BizProductionPlanDetailSubDetailEquipmentUser;
import cn.ezhizao.project.business.service.IBizProductionPlanDetailSubDetailEquipmentUserService;
/**
 * 子计划对应的工序指定的使用设备Controller
 *
 * @author ezhizao
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/business/equipmentUser")
public class BizProductionPlanDetailSubDetailEquipmentUserController extends BaseController
{
    @Resource
    private IBizProductionPlanDetailSubDetailEquipmentUserService bizProductionPlanDetailSubDetailEquipmentUserService;
    @Resource
    HttpServletRequest request;

    /**
     * 查询子计划对应的工序指定的使用设备列表
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizProductionPlanDetailSubDetailEquipmentUser bizProductionPlanDetailSubDetailEquipmentUser) throws NoSuchFieldException, IllegalAccessException
    {
//        String tenantId = request.getHeader("tenantId");
//        bizProductionPlanDetailSubDetailEquipmentUser.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProductionPlanDetailSubDetailEquipmentUser> list = bizProductionPlanDetailSubDetailEquipmentUserService.getList(bizProductionPlanDetailSubDetailEquipmentUser);
        return getDataTable(list);
    }

    /**
     * 导出子计划对应的工序指定的使用设备列表
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentUser:export')")
    @Log(title = "子计划对应的工序指定的使用设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProductionPlanDetailSubDetailEquipmentUser bizProductionPlanDetailSubDetailEquipmentUser) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProductionPlanDetailSubDetailEquipmentUser);
        List<BizProductionPlanDetailSubDetailEquipmentUser> list = bizProductionPlanDetailSubDetailEquipmentUserService.getList(bizProductionPlanDetailSubDetailEquipmentUser);
        ExcelUtil<BizProductionPlanDetailSubDetailEquipmentUser> util = new ExcelUtil<BizProductionPlanDetailSubDetailEquipmentUser>(BizProductionPlanDetailSubDetailEquipmentUser.class);
        util.exportExcel(response, list, "子计划对应的工序指定的使用设备数据");
    }

    /**
     * 获取子计划对应的工序指定的使用设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizProductionPlanDetailSubDetailEquipmentUserService.getById(id));
    }

    /**
     * 新增子计划对应的工序指定的使用设备
     */
    //@PreAuthorize("@ss.hasPermi('business:equipmentUser:add')")
    @Log(title = "子计划对应的工序指定的使用设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@RequestBody List<BizProductionPlanDetailSubDetailEquipmentUser> bizProductionPlanDetailSubDetailEquipmentUser) throws NoSuchFieldException, IllegalAccessException
    {
//        String tenantId = request.getHeader("tenantId");

//        if(bizProductionPlanDetailSubDetailEquipmentUser.size()>0){
//            BizProductionPlanDetailSubDetailEquipmentUser equipmentUser = new BizProductionPlanDetailSubDetailEquipmentUser();
//            equipmentUser.setProductionPlanDetailSubDetailEquipmentId(bizProductionPlanDetailSubDetailEquipmentUser.get(0).getProductionPlanDetailSubDetailEquipmentId());
//            equipmentUser.setEquipmentDetailId(bizProductionPlanDetailSubDetailEquipmentUser.get(0).getEquipmentDetailId());
//            equipmentUser.setEquipmentDetailCode(bizProductionPlanDetailSubDetailEquipmentUser.get(0).getEquipmentDetailCode());
//            List<BizProductionPlanDetailSubDetailEquipmentUser> oldList = bizProductionPlanDetailSubDetailEquipmentUserService.getList(equipmentUser);
//
//            return toAjax(saveOrUpdateBatch(bizProductionPlanDetailSubDetailEquipmentUserService,bizProductionPlanDetailSubDetailEquipmentUser,oldList));
//        }
        List<Long> ids = new ArrayList<>();
        for (BizProductionPlanDetailSubDetailEquipmentUser item: bizProductionPlanDetailSubDetailEquipmentUser
             ) {
            ids.add(item.getProductionPlanDetailSubDetailEquipmentId());
//           item.setTenantId(Long.valueOf(tenantId));
        }
        ids = ids.stream().distinct().collect(Collectors.toList());
        QueryWrapper<BizProductionPlanDetailSubDetailEquipmentUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("production_plan_detail_sub_detail_equipment_id", ids);
//        queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
        List<BizProductionPlanDetailSubDetailEquipmentUser> oldList = bizProductionPlanDetailSubDetailEquipmentUserService.list(queryWrapper);


        return toAjax(saveOrUpdateBatch(bizProductionPlanDetailSubDetailEquipmentUserService,bizProductionPlanDetailSubDetailEquipmentUser,oldList));
    }

    /**
     * 修改子计划对应的工序指定的使用设备
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentUser:edit')")
    @Log(title = "子计划对应的工序指定的使用设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProductionPlanDetailSubDetailEquipmentUser bizProductionPlanDetailSubDetailEquipmentUser) throws NoSuchFieldException, IllegalAccessException
    {
//        String tenantId = request.getHeader("tenantId");
//        bizProductionPlanDetailSubDetailEquipmentUser.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizProductionPlanDetailSubDetailEquipmentUserService.updateById(bizProductionPlanDetailSubDetailEquipmentUser));
    }

    /**
     * 删除子计划对应的工序指定的使用设备
     */
    @PreAuthorize("@ss.hasPermi('business:equipmentUser:remove')")
    @Log(title = "子计划对应的工序指定的使用设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizProductionPlanDetailSubDetailEquipmentUserService.removeBatchByIds(ids));
    }
}
