package com.ruoyi.asset.controller;

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

import cn.hutool.core.util.StrUtil;
import com.ruoyi.asset.domain.dto.TbBorrowRecordDTO;
import com.ruoyi.asset.domain.dto.TbCountAssetDTO;
import com.ruoyi.asset.utils.RegexUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.TbAssetBorrowRecord;
import com.ruoyi.asset.service.ITbAssetBorrowRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备借用记录Controller
 * 
 * @author 原动力
 * @date 2023-03-27
 */
@Api(tags = "设备借用记录")
@RestController
@RequestMapping("/asset/borrow")
public class TbAssetBorrowRecordController extends BaseController
{
    @Autowired
    private ITbAssetBorrowRecordService tbAssetBorrowRecordService;

    /**
     * 查询设备借用记录列表
     */
    @ApiOperation("查询设备借用记录列表")
    @PreAuthorize("@ss.hasPermi('asset:borrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        startPage();
        List<TbBorrowRecordDTO> list = tbAssetBorrowRecordService.selectTbAssetBorrowRecordDTOList(tbAssetBorrowRecord);
        return getDataTable(list);
    }

    /**
     * 导出设备借用记录列表
     */
    @ApiOperation("导出设备借用记录列表")
    @PreAuthorize("@ss.hasPermi('asset:borrow:export')")
    @Log(title = "设备借用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        List<TbBorrowRecordDTO> list = tbAssetBorrowRecordService.selectTbAssetBorrowRecordDTOList(tbAssetBorrowRecord);
        ExcelUtil<TbBorrowRecordDTO> util = new ExcelUtil<TbBorrowRecordDTO>(TbBorrowRecordDTO.class);
        util.exportExcel(response, list, "设备借用记录数据");
    }

    /**
     * 获取设备借用记录详细信息
     */
    @ApiOperation("获取设备借用记录详细信息")
    @PreAuthorize("@ss.hasPermi('asset:borrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbAssetBorrowRecordService.selectTbAssetBorrowRecordById(id));
    }

    /**
     * 新增设备借用记录
     */
    @ApiOperation("新增设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:add')")
    @Log(title = "设备借用记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        return toAjax(tbAssetBorrowRecordService.insertTbAssetBorrowRecord(tbAssetBorrowRecord));
    }

    /**
     * 修改设备借用记录
     */
    @ApiOperation("修改设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:edit')")
    @Log(title = "设备借用记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        return toAjax(tbAssetBorrowRecordService.updateTbAssetBorrowRecord(tbAssetBorrowRecord));
    }

    /**
     * 删除设备借用记录
     */
    @ApiOperation("删除设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:remove')")
    @Log(title = "设备借用记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbAssetBorrowRecordService.deleteTbAssetBorrowRecordByIds(ids));
    }

    /**
     * 根据分类统计借用次数
     */
    @ApiOperation("各分类借用统计")
    @PreAuthorize("@ss.hasPermi('asset:borrow:list')")
    @GetMapping("/countList")
    public AjaxResult countList()
    {
        List<TbCountAssetDTO> countAssetDto = tbAssetBorrowRecordService.countAssetDto();
        return new AjaxResult().success(countAssetDto);
    }

    /**
     * 小程序：新增设备借用记录
     */
    @PreAuthorize("@ss.hasPermi('asset:borrow:add')")
    @Log(title = "设备借用记录", businessType = BusinessType.INSERT)
    @PostMapping("/save/{borrowTime}/{returnTime}")
    public AjaxResult saveBorrowRecord(@RequestBody TbAssetBorrowRecord tbAssetBorrowRecord, @PathVariable("borrowTime") Long borrowTime, @PathVariable("returnTime") Long returnTime)
    {
        String assetBarCode = tbAssetBorrowRecord.getAssetBarCode();
        if (StrUtil.isBlank(assetBarCode)) {
            return AjaxResult.error("资产设备条形码不能为空");
        }
        String borrowPlaceName = tbAssetBorrowRecord.getBorrowPlaceName();
        if (StrUtil.isBlank(borrowPlaceName)) {
            return AjaxResult.error("借用地点不能为空");
        }
        String tel = tbAssetBorrowRecord.getTel();
        if (StrUtil.isBlank(tel)) {
            return AjaxResult.error("联系电话不能为空");
        }
        if (RegexUtils.isPhoneInvalid(tel)) {
            return AjaxResult.error("联系电话格式错误");
        }
        Date nowDate = new Date();
        Date borrowDate = new Date(borrowTime);
        if (nowDate.after(borrowDate)) {
            return AjaxResult.error("借用时间不能早于当前时间");
        }
        tbAssetBorrowRecord.setBorrowDate(borrowDate);
        Date returnDate = new Date(returnTime);
        if (!returnDate.after(borrowDate)) {
            return AjaxResult.error("归还时间不能早于借用时间");
        }
        if (returnDate.equals(borrowDate)) {
            return AjaxResult.error("归还时间不能与借用时间相等");
        }
        tbAssetBorrowRecord.setReturnDate(returnDate);

        return tbAssetBorrowRecordService.saveBorrowRecord(tbAssetBorrowRecord);
    }

    /**
     * 修改设备借用记录为归还
     */
    @PreAuthorize("@ss.hasPermi('asset:borrow:add')")
    @Log(title = "修改设备借用记录为归还", businessType = BusinessType.UPDATE)
    @PutMapping("/return/{barCode}")
    public AjaxResult updateBorrowRecordToReturn(@PathVariable("barCode") String barCode)
    {
        return tbAssetBorrowRecordService.updateBorrowRecordToReturn(barCode);
    }

    /**
     * 获取用户自己设备借用记录
     */
    @ApiOperation("获取用户自己设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:list')")
    @GetMapping("/recordList")
    public TableDataInfo selectMeBorrowRecordList(@RequestParam(value = "status", required = false) Long status)
    {
        startPage();
        List<TbAssetBorrowRecord> tbAssetBorrowRecords = tbAssetBorrowRecordService.selectMeTbAssetBorrowRecord(status);
        return getDataTable(tbAssetBorrowRecords);
    }

    /**
     * 获取用户自己设备借用记录详细
     */
    @ApiOperation("获取用户自己设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:list')")
    @GetMapping("/record/{id}")
    public AjaxResult selectMeBorrowRecord(@PathVariable("id") Long id)
    {
        TbBorrowRecordDTO tbAssetBorrowRecord = tbAssetBorrowRecordService.selectMeTbAssetBorrowRecordById(id);
        return AjaxResult.success("查询成功", tbAssetBorrowRecord);
    }

    /**
     * 统计用户自己设备借用记录
     */
    @ApiOperation("统计用户自己设备借用记录")
    @PreAuthorize("@ss.hasPermi('asset:borrow:list')")
    @GetMapping("/recordCount")
    public AjaxResult countMeTbAssetBorrow(@RequestParam(value = "status", required = false) Long status)
    {
        return tbAssetBorrowRecordService.countMeTbAssetBorrowRecord(status);
    }

}
