package com.willalp.web.controller.device;

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.enums.NumberRuleEnum;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.device.domain.Device;
import com.willalp.device.service.IDeviceService;
import com.willalp.number.domain.req.NumberRuleReq;
import com.willalp.number.service.IHsSysNumberRuleService;
import io.swagger.annotations.Api;
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;

/**
 * deviceController
 *
 * @author willalp
 * @date 2021-11-23
 */
@Api("app配置控制")
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;

    /**
     * 查询device列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出device列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:export')")
    @Log(title = "device" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        return util.exportExcel(list, "device数据");
    }

    /**
     * 获取device详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") String deviceId) {
        return AjaxResult.success(deviceService.getById(deviceId));
    }

    /**
     * 新增device
     */
    @PreAuthorize("@ss.hasPermi('device:device:add')")
    @Log(title = "device" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            Device byId = deviceService.getById(idStr);
            if (null == byId) {
                device.setDeviceId(idStr);
                break;
            }
        }
        //编号
        NumberRuleReq numberRuleReq =new NumberRuleReq();
        numberRuleReq.setRuleId(NumberRuleEnum.DEVICE.getValue());
        String number = hsSysNumberRuleService.getNumber(numberRuleReq);
        device.setDeviceNo(number);
        device.setCreateBy(SecurityUtils.getUsername());
        device.setCreateTime(DateUtils.getNowDate());
        return toAjax(deviceService.save(device));
    }

    /**
     * 修改device
     */
    @PreAuthorize("@ss.hasPermi('device:device:edit')")
    @Log(title = "device" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device) {
        device.setUpdateBy(SecurityUtils.getUsername());
        device.setUpdateTime(DateUtils.getNowDate());
        return toAjax(deviceService.updateById(device));
    }

    /**
     * 删除device
     */
    @PreAuthorize("@ss.hasPermi('device:device:remove')")
    @Log(title = "device" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable String[] deviceIds) {
        return toAjax(deviceService.removeByIds(Arrays.asList(deviceIds)));
    }


}
