package cn.ezhizao.project.business.outsourceBalanceMonth.controller;

import java.util.Date;
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.outsourceBalanceMonth.domain.BizOutsourceBalanceMonth;
import cn.ezhizao.project.business.outsourceBalanceMonth.service.IBizOutsourceBalanceMonthService;
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;

/**
 * 外协结算月设置Controller
 *
 * @author ezhizao
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/business/outsourceBalanceMonth")
public class BizOutsourceBalanceMonthController extends BaseController
{
    @Resource
    private IBizOutsourceBalanceMonthService bizOutsourceBalanceMonthService;

    /**
     * 查询外协结算月设置列表
     */
    @GetMapping("/list")
    @Log(title = "外协结算月设置查询", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizOutsourceBalanceMonth bizOutsourceBalanceMonth) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(bizOutsourceBalanceMonth);
        startPage();
        List<BizOutsourceBalanceMonth> list = bizOutsourceBalanceMonthService.getList(bizOutsourceBalanceMonth);
        return getDataTable(list);
    }

    /**
     * 导出外协结算月设置列表
     */
    @Log(title = "外协结算月设置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizOutsourceBalanceMonth bizOutsourceBalanceMonth) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizOutsourceBalanceMonth);
        List<BizOutsourceBalanceMonth> list = bizOutsourceBalanceMonthService.getList(bizOutsourceBalanceMonth);
        ExcelUtil<BizOutsourceBalanceMonth> util = new ExcelUtil<BizOutsourceBalanceMonth>(BizOutsourceBalanceMonth.class);
        util.exportExcel(response, list, "外协结算月设置数据");
    }

    /**
     * 获取外协结算月设置详细信息
     */
    @GetMapping(value = "/{id}")
    @Log(title = "外协结算月设置查询详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizOutsourceBalanceMonthService.getById(id));
    }

    /**
     * 新增外协结算月设置
     */
    @Log(title = "外协结算月设置新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizOutsourceBalanceMonth bizOutsourceBalanceMonth) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizOutsourceBalanceMonth);
        return toAjax(bizOutsourceBalanceMonthService.save(bizOutsourceBalanceMonth));
    }

    /**
     * 修改外协结算月设置
     */
    @Log(title = "外协结算月设置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizOutsourceBalanceMonth bizOutsourceBalanceMonth) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizOutsourceBalanceMonth);
        return toAjax(bizOutsourceBalanceMonthService.updateById(bizOutsourceBalanceMonth));
    }

    /**
     * 删除外协结算月设置
     */
    @Log(title = "外协结算月设置删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizOutsourceBalanceMonthService.removeBatchByIds(ids));
    }

    /**
     * 修改外协结算月设置
     */
    @Log(title = "外协结算月设置保存", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BizOutsourceBalanceMonth bizOutsourceBalanceMonth) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizOutsourceBalanceMonth);
        bizOutsourceBalanceMonth.setUserId(getUserId());
        List<BizOutsourceBalanceMonth> month = bizOutsourceBalanceMonthService.query().isNull("stop_date").list();
        month.forEach(l -> l.setStopDate(new Date()));
        bizOutsourceBalanceMonthService.updateBatchById(month);
        return toAjax(bizOutsourceBalanceMonthService.save(bizOutsourceBalanceMonth));
    }
}
