package cn.ezhizao.project.business.controller;

import java.util.List;
import javax.annotation.Resource;
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 cn.ezhizao.project.system.domain.SysRole;
import cn.ezhizao.project.system.domain.SysUserRole;
import cn.ezhizao.project.system.service.ISysRoleService;
import cn.ezhizao.project.system.service.ISysUserRoleService;
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.BizPackageResourceGroupDetail;
import cn.ezhizao.project.business.service.IBizPackageResourceGroupDetailService;

/**
 * 包装线明细Controller
 *
 * @author ezhizao
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/business/packageResourceGroupDetail")
public class BizPackageResourceGroupDetailController extends BaseController
{
    @Resource
    private IBizPackageResourceGroupDetailService bizPackageResourceGroupDetailService;
    @Resource
    private ISysUserRoleService sysUserRoleService;
    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 查询包装线明细列表
     */
    @GetMapping("/list")
    @Log(title = "查询包装线明细列表", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizPackageResourceGroupDetail bizPackageResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizPackageResourceGroupDetail);
        startPage();
        List<BizPackageResourceGroupDetail> list = bizPackageResourceGroupDetailService.getList(bizPackageResourceGroupDetail);
        return getDataTable(list);
    }

    /**
     * 导出包装线明细列表
     */
    @PreAuthorize("@ss.hasPermi('business:packageResourceGroupDetail:export')")
    @Log(title = "导出包装线明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPackageResourceGroupDetail bizPackageResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizPackageResourceGroupDetail);
        List<BizPackageResourceGroupDetail> list = bizPackageResourceGroupDetailService.getList(bizPackageResourceGroupDetail);
        ExcelUtil<BizPackageResourceGroupDetail> util = new ExcelUtil<BizPackageResourceGroupDetail>(BizPackageResourceGroupDetail.class);
        util.exportExcel(response, list, "包装线明细数据");
    }

    /**
     * 获取包装线明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:packageResourceGroupDetail:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "获取包装线明细详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizPackageResourceGroupDetailService.getById(id));
    }

    /**
     * 新增包装线明细
     */
    @Log(title = "新增包装线明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPackageResourceGroupDetail bizPackageResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizPackageResourceGroupDetail);
        return toAjax(bizPackageResourceGroupDetailService.save(bizPackageResourceGroupDetail));
    }

    /**
     * 修改包装线明细
     */
    @Log(title = "修改包装线明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPackageResourceGroupDetail bizPackageResourceGroupDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizPackageResourceGroupDetail);
        return toAjax(bizPackageResourceGroupDetailService.updateById(bizPackageResourceGroupDetail));
    }

    /**
     * 删除包装线明细
     */
    @Log(title = "删除包装线明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        List<BizPackageResourceGroupDetail> details = bizPackageResourceGroupDetailService.query().in("id", ids).list();
        // 判断删除后是否员工没有包装线，假设已经没有包装线，则删除包装角色
        boolean result = bizPackageResourceGroupDetailService.removeBatchByIds(ids);
        // 同时删除对应的权限
        SysRole role = sysRoleService.getRoleByKey("certificate");
        details.forEach(l -> {
            SysUserRole delRole = new SysUserRole();
            delRole.setUserId(l.getUserId());
            delRole.setRoleId(role.getRoleId());
            sysUserRoleService.deleteUserRoleInfo(delRole);
        });
        return toAjax(true);
    }
}
