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.BizProductionResourceGroup;

import cn.ezhizao.project.business.domain.BizProductionResourceGroupDetail;
import cn.ezhizao.project.business.service.IBizProductionResourceGroupDetailService;
import cn.ezhizao.project.business.service.IBizProductionResourceGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 工段资源组Controller
 *
 * @author ezhizao
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/business/resourceGroup")
public class BizProductionResourceGroupController extends BaseController
{
    @Resource
    private IBizProductionResourceGroupService bizProductionResourceGroupService;

    @Resource
    private IBizProductionResourceGroupDetailService bizProductionResourceGroupDetailService;

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

        List<BizProductionResourceGroup> list = bizProductionResourceGroupService.getList(bizProductionResourceGroup);
        return getDataTable(list);
    }

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

    /**
     * 获取工段资源组详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BizProductionResourceGroup bizProductionResourceGroup = new BizProductionResourceGroup();
        bizProductionResourceGroup.setId(id);
        return success(bizProductionResourceGroupService.getList(bizProductionResourceGroup).get(0));
    }

    /**
     * 新增工段资源组
     */
    @Log(title = "工段资源组", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody BizProductionResourceGroup bizProductionResourceGroup) throws NoSuchFieldException, IllegalAccessException
    {
        bizProductionResourceGroupService.saveOrUpdate(bizProductionResourceGroup);
        for (BizProductionResourceGroupDetail item: bizProductionResourceGroup.getGroupDetailList()
             ) {
            item.setProductionResourceGroupId(bizProductionResourceGroup.getId());
        }
        List<BizProductionResourceGroupDetail> oldList = bizProductionResourceGroupDetailService.query().eq("production_resource_group_id", bizProductionResourceGroup.getId()).list();
        boolean batch = saveOrUpdateBatch(bizProductionResourceGroupDetailService, bizProductionResourceGroup.getGroupDetailList(), oldList);
        return toAjax(batch);
    }

    /**
     * 修改工段资源组
     */
    @Log(title = "工段资源组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProductionResourceGroup bizProductionResourceGroup) throws NoSuchFieldException, IllegalAccessException
    {
        return toAjax(bizProductionResourceGroupService.updateById(bizProductionResourceGroup));
    }

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