package cn.ezhizao.project.business.innerTurnover.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.business.innerTurnover.domain.BizInnerTurnover;
import cn.ezhizao.project.business.innerTurnover.service.IBizInnerTurnoverService;
import cn.ezhizao.project.system.domain.SysDept;
import cn.ezhizao.project.system.service.impl.SysDeptServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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;


/**
 * 工段内部人周转表Controller
 *
 * @author ezhizao
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/business/innerTurnover")
public class BizInnerTurnoverController extends BaseController
{
    @Resource
    private IBizInnerTurnoverService bizInnerTurnoverService;
    @Resource
    private SysDeptServiceImpl sysDeptService;

    /**
     * 查询工段内部人周转表列表
     */
    @GetMapping("/list")
    @Log(title = "工段内部人周转查询", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizInnerTurnover bizInnerTurnover) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizInnerTurnover);
        List<BizInnerTurnover> list = bizInnerTurnoverService.getList(bizInnerTurnover);
        return getDataTable(list);
    }

    /**
     * 导出工段内部人周转表列表
     */
    @PreAuthorize("@ss.hasPermi('business:innerTurnover:export')")
    @Log(title = "工段内部人周转导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizInnerTurnover bizInnerTurnover) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizInnerTurnover);
        List<BizInnerTurnover> list = bizInnerTurnoverService.getList(bizInnerTurnover);
        ExcelUtil<BizInnerTurnover> util = new ExcelUtil<BizInnerTurnover>(BizInnerTurnover.class);
        util.exportExcel(response, list, "工段内部人周转表数据");
    }

    /**
     * 获取工段内部人周转表详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:innerTurnover:query')")
    @Log(title = "工段内部人周转查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizInnerTurnoverService.getById(id));
    }

    /**
     * 新增工段内部人周转表
     */
    @Log(title = "工段内部人周转新增", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody SysDept sysDept) throws NoSuchFieldException, IllegalAccessException
    {
        SysDept dept = sysDeptService.getDeptByDeptId(sysDept.getDeptId());
        dept.setIsCentralizedTurnover(sysDept.getIsCentralizedTurnover());
        //修改dept表
        sysDeptService.updateDept(dept);
        //操作内部周转表
        List<BizInnerTurnover>oldList = bizInnerTurnoverService.query().eq("dept_id", dept.getDeptId()).list();
        saveOrUpdateBatch(bizInnerTurnoverService,sysDept.getInnerTurnoverList(),oldList);
        return success();
    }

    /**
     * 修改工段内部人周转表
     */
    @PreAuthorize("@ss.hasPermi('business:innerTurnover:edit')")
    @Log(title = "工段内部人周转修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizInnerTurnover bizInnerTurnover) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizInnerTurnover);
        return toAjax(bizInnerTurnoverService.updateById(bizInnerTurnover));
    }

    /**
     * 删除工段内部人周转表
     */
    @PreAuthorize("@ss.hasPermi('business:innerTurnover:remove')")
    @Log(title = "工段内部人周转删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizInnerTurnoverService.removeBatchByIds(ids));
    }
}
