package com.willalp.web.controller.number;

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.number.domain.HsSysNumberRule;
import com.willalp.number.service.IHsSysNumberRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 编号规则Controller
 *
 * @author songyu
 * @date 2021-11-24
 */
@RestController
@RequestMapping("/number/rule")
public class HsSysNumberRuleController extends BaseController
{
    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;

    /**
     * 查询编号规则列表
     */
    @PreAuthorize("@ss.hasPermi('number:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsSysNumberRule hsSysNumberRule)
    {
        startPage();
        List<HsSysNumberRule> list = hsSysNumberRuleService.selectHsSysNumberRuleList(hsSysNumberRule);
        return getDataTable(list);
    }

    /**
     * 导出编号规则列表
     */
    @PreAuthorize("@ss.hasPermi('number:rule:export')")
    @Log(title = "编号规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsSysNumberRule hsSysNumberRule)
    {
        List<HsSysNumberRule> list = hsSysNumberRuleService.selectHsSysNumberRuleList(hsSysNumberRule);
        ExcelUtil<HsSysNumberRule> util = new ExcelUtil<HsSysNumberRule>(HsSysNumberRule.class);
        return util.exportExcel(list, "编号规则数据");
    }

    /**
     * 获取编号规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('number:rule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") String ruleId)
    {
        return AjaxResult.success(hsSysNumberRuleService.getById(ruleId));
    }

    /**
     * 新增编号规则
     */
    @PreAuthorize("@ss.hasPermi('number:rule:add')")
    @Log(title = "编号规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsSysNumberRule hsSysNumberRule)
    {
//        while (true) {
//            String idStr = IdWorker.getIdStr();
//            HsSysNumberRule byId = hsSysNumberRuleService.getById(idStr);
//            if (null == byId) {
//                hsSysNumberRule.setRuleId(idStr);
//                break;
//            }
//        }
        hsSysNumberRule.setCreateBy(SecurityUtils.getUsername());
        hsSysNumberRule.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsSysNumberRuleService.save(hsSysNumberRule));
    }

    /**
     * 修改编号规则
     */
    @PreAuthorize("@ss.hasPermi('number:rule:edit')")
    @Log(title = "编号规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsSysNumberRule hsSysNumberRule)
    {
        hsSysNumberRule.setUpdateBy(SecurityUtils.getUsername());
        hsSysNumberRule.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsSysNumberRuleService.updateById(hsSysNumberRule));
    }

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