package com.yunyan.web.controller.business;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yunyan.common.core.domain.entity.SysDept;
import com.yunyan.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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 com.yunyan.common.annotation.Log;
import com.yunyan.common.core.controller.BaseController;
import com.yunyan.common.core.domain.AjaxResult;
import com.yunyan.common.enums.BusinessType;
import com.yunyan.business.domain.FillDate;
import com.yunyan.business.service.IFillDateService;
import com.yunyan.common.utils.poi.ExcelUtil;
import com.yunyan.common.core.page.TableDataInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yunyan.common.core.domain.AjaxResultVo;
import com.yunyan.common.utils.SecurityUtils;
import java.util.Date;

/**
 * 报告期限设置Controller
 *
 * @author yunyan
 * @date 2023-07-27
 */
@Api(tags = "4.0.3报告期限设置")
@RestController
@RequestMapping("/business/fillDate")
public class FillDateController extends BaseController
{
    @Autowired
    private IFillDateService fillDateService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询报告期限设置列表
     */
    @ApiOperation(value="查询报告期限设置列表", notes="查询报告期限设置列表")
    @PreAuthorize("@ss.hasPermi('business:fillDate:list')")
    @GetMapping("/list")
    public TableDataInfo<FillDate> list(FillDate fillDate)
    {
        if(!SecurityUtils.getLoginUser().getUser().isAdmin()) {
            List<Long> deptIdList = SecurityUtils.getDeptIdList();
            if(!deptIdList.isEmpty()) {
                SysDept sysDept = sysDeptService.getById(deptIdList.get(0));
                if(null != sysDept) {
                    if(sysDept.getDeptLevel() == 2) {
                        fillDate.getParams().put("level1", sysDept.getDeptId());
                        fillDate.getParams().put("deptParentIdCurrent", sysDept.getDeptId());
                        fillDate.getParams().put("deptLevelSelect", 8);
                    } else if(sysDept.getDeptLevel() == 8) {
                        fillDate.getParams().put("level2", sysDept.getDeptId());
                        fillDate.getParams().put("deptParentIdCurrent", sysDept.getParentId());
                        fillDate.getParams().put("deptIdCurrent", sysDept.getDeptId());
                    }
                }
                if(null == fillDate.getParams().get("level1") && null == fillDate.getParams().get("level2") ) {
                    fillDate.getParams().put("deptIdOther", -1L);
                }
            } else {
                fillDate.getParams().put("deptIdOther", -1L);
            }
        }


        startPage();
        List<FillDate> list = fillDateService.selectFillDateList(fillDate);
        return getDataTable(list);
    }

    /**
     * 导出报告期限设置列表
     */
    @PreAuthorize("@ss.hasPermi('business:fillDate:export')")
    @Log(title = "报告期限设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FillDate fillDate)
    {
        List<FillDate> list = fillDateService.selectFillDateList(fillDate);
        ExcelUtil<FillDate> util = new ExcelUtil<FillDate>(FillDate.class);
        util.exportExcel(response, list, "报告期限设置数据");
    }

    /**
     * 获取报告期限设置详细信息
     */
    @ApiOperation(value="获取报告期限设置详细信息", notes="获取报告期限设置详细信息")
    @PreAuthorize("@ss.hasPermi('business:fillDate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResultVo getInfo(@PathVariable("id") Long id)
    {
        return AjaxResultVo.success(fillDateService.getById(id));
    }

    /**
     * 新增报告期限设置
     */
    @ApiOperation(value="新增报告期限设置", notes="新增报告期限设置")
    @PreAuthorize("@ss.hasPermi('business:fillDate:add')")
    @Log(title = "报告期限设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResultVo add(@RequestBody FillDate dto)
    {
        return fillDateService.add(dto);
        //return toAjax(fillDateService.save(dto));
    }

    /**
     * 修改报告期限设置
     */
    @ApiOperation(value="修改报告期限设置", notes="修改报告期限设置")
    @PreAuthorize("@ss.hasPermi('business:fillDate:edit')")
    @Log(title = "报告期限设置", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FillDate dto)
    {
        return toAjax(fillDateService.updateById(dto));
    }

    /**
     * 删除报告期限设置
     */
    @ApiOperation(value="删除报告期限设置", notes="删除报告期限设置")
    @PreAuthorize("@ss.hasPermi('business:fillDate:remove')")
    @Log(title = "报告期限设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fillDateService.removeByIds(Arrays.asList(ids)));
    }

}
