package cn.ezhizao.project.business.controller;

import java.util.List;
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.BizResourceAllocation;
import cn.ezhizao.project.business.service.IBizResourceAllocationService;

/**
 * 资源分配Controller
 *
 * @author ezhizao
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/business/resourceAllocation")
public class BizResourceAllocationController extends BaseController {
    @Resource
    private IBizResourceAllocationService bizResourceAllocationService;
    @Resource
    HttpServletRequest request;

    /**
     * 查询资源分配列表
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizResourceAllocation bizResourceAllocation) throws NoSuchFieldException, IllegalAccessException {
        String tenantId = request.getHeader("tenantId");
        bizResourceAllocation.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizResourceAllocation> list = bizResourceAllocationService.getList(bizResourceAllocation);
        return getDataTable(list);
    }

    /**
     * 导出资源分配列表
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:export')")
    @Log(title = "资源分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizResourceAllocation bizResourceAllocation) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizResourceAllocation);
        List<BizResourceAllocation> list = bizResourceAllocationService.getList(bizResourceAllocation);
        ExcelUtil<BizResourceAllocation> util = new ExcelUtil<BizResourceAllocation>(BizResourceAllocation.class);
        util.exportExcel(response, list, "资源分配数据");
    }

    /**
     * 获取资源分配详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizResourceAllocationService.getById(id));
    }

    /**
     * 新增资源分配
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:add')")
    @Log(title = "资源分配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@RequestBody List<BizResourceAllocation> bizResourceAllocation) throws NoSuchFieldException, IllegalAccessException {
        String tenantId = request.getHeader("tenantId");
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!"0".equals(tenantId)){
            queryWrapper.eq("tenant_id", Long.valueOf(tenantId));
        }
        queryWrapper.eq("dept_user_id", bizResourceAllocation.get(0).getDeptUserId());
        List<BizResourceAllocation> oldList = bizResourceAllocationService.list(queryWrapper);
        for (BizResourceAllocation item : bizResourceAllocation) {
            item.setTenantId(Long.valueOf(tenantId));
        }

        return toAjax(saveOrUpdateBatch(bizResourceAllocationService, bizResourceAllocation, oldList));
    }

    /**
     * 修改资源分配
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:edit')")
    @Log(title = "资源分配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizResourceAllocation bizResourceAllocation) throws NoSuchFieldException, IllegalAccessException {
        String tenantId = request.getHeader("tenantId");
        bizResourceAllocation.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizResourceAllocationService.updateById(bizResourceAllocation));
    }

    /**
     * 删除资源分配
     */
    @PreAuthorize("@ss.hasPermi('business:resourceAllocation:remove')")
    @Log(title = "资源分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizResourceAllocationService.removeBatchByIds(ids));
    }
}
