package com.willalp.web.controller.yiqing;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.yiqing.domain.HsYiqingRule;
import com.willalp.yiqing.service.IHsYiqingRuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 任务提醒规则Controller
 *
 * @author willalp
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/yiqing/rule")
public class HsYiqingRuleController extends BaseController {
    @Resource
    private IHsYiqingRuleService hsYiqingRuleService;

    /**
     * 查询任务提醒规则列表
     */
    @GetMapping("/listAll")
    public AjaxResult selectAll() {
        return AjaxResult.success(hsYiqingRuleService.selectHsYiqingRuleList(null));
    }

    /**
     * 查询任务提醒规则列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsYiqingRule hsYiqingRule) {
        startPage();
        List<HsYiqingRule> list = hsYiqingRuleService.selectHsYiqingRuleList(hsYiqingRule);
        return getDataTable(list);
    }

    /**
     * 导出任务提醒规则列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:export')")
    @Log(title = "任务提醒规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingRule hsYiqingRule) {
        List<HsYiqingRule> list = hsYiqingRuleService.selectHsYiqingRuleList(hsYiqingRule);
        ExcelUtil<HsYiqingRule> util = new ExcelUtil<HsYiqingRule>(HsYiqingRule.class);
        return util.exportExcel(list, "任务提醒规则数据");
    }

    /**
     * 获取任务提醒规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") String ruleId) {
        return AjaxResult.success(hsYiqingRuleService.getById(ruleId));
    }

    /**
     * 新增任务提醒规则
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:add')")
    @Log(title = "任务提醒规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingRule hsYiqingRule) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsYiqingRule byId = hsYiqingRuleService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsYiqingRule.setRuleId(idStr);
                break;
            }
        }
        hsYiqingRule.setCreateBy(SecurityUtils.getUsername());
        hsYiqingRule.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingRuleService.save(hsYiqingRule));
    }

    /**
     * 修改任务提醒规则
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:edit')")
    @Log(title = "任务提醒规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingRule hsYiqingRule) {
        hsYiqingRule.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingRule.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingRuleService.updateById(hsYiqingRule));
    }

    /**
     * 删除任务提醒规则
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rule:remove')")
    @Log(title = "任务提醒规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable String[] ruleIds) {
        return toAjax(hsYiqingRuleService.removeByIds(Arrays.asList(ruleIds)));
    }
}
