package com.zd.chemical.controller;

import com.zd.chemical.domain.HxpUserecord;
import com.zd.chemical.domain.vo.*;
import com.zd.chemical.service.IHxpUserecordService;
import com.zd.chemical.util.SmsSydUtil;
import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 化学品使用记录Controller
 *
 * @author liubo
 * @date 2022-06-18
 */
@RestController
@Api(tags = "【化学品使用记录】")
@RequestMapping("/hxpUserecord")
public class HxpUserecordController extends BaseController {
    @Autowired
    private IHxpUserecordService hxpUserecordService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SmsSydUtil smsSydUtil;

    /**
     * 查询化学品使用记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_USERECORD+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询化学品使用记录列表")
    public TableDataInfo<HxpUserecord> list(HxpUserecord hxpUserecord) {
        startPage();
        List<HxpUserecord> list = hxpUserecordService.selectHxpUserecordList(hxpUserecord);
        return getDataTable(list);
    }

    /**
     * 查询化学品使用记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_USERECORD+ PerFun.LIST)
    @GetMapping("/getHxpUserecordList")
    @ApiOperation(value = "查询化学品使用记录列表")
    public TableDataInfo<HxpUserecordVo> getHxpUserecordList(HxpUserecordSearch hxpUserecordSearch) {
        startPage();
        List<HxpUserecordVo> list = hxpUserecordService.getHxpUserecordList(hxpUserecordSearch);
        return getDataTable(list);
    }

    @GetMapping("/getHxpUserecordListByUser")
    @ApiOperation(value = "学生端查询化学品使用记录列表")
    public TableDataInfo<HxpUserecordVo> getHxpUserecordListByUser(HxpUserecordSearch hxpUserecordSearch) {
        startPage();
        hxpUserecordSearch.setLoginUserId(tokenService.getLoginUser().getUserid());
        List<HxpUserecordVo> list = hxpUserecordService.getHxpUserecordList(hxpUserecordSearch);
        return getDataTable(list);
    }

    /**
     * 获取化学品使用记录详细信息
     */
    @ApiOperation(value = "获取化学品使用记录详细信息")
    @GetMapping(value = "/queryDetail/{id}")
    public R<HxpUserecordDetailVo> queryDetailById(@PathVariable("id") Long id) {
        return R.ok(hxpUserecordService.queryDetailById(id));
    }

    /**
     * 导出化学品使用记录列表
     */
    @ApiOperation(value = "导出化学品使用记录列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_USERECORD+ PerFun.EXPORT)
    @Log(title = "化学品使用记录", businessType = BusinessType.EXPORT)
    @PostMapping({"/export","/export/{ids}"})
    public void export(HttpServletResponse response, HxpUserecordSearch hxpUserecordSearch, @PathVariable(value = "ids",required = false) Long... ids) throws IOException {
        if(ids!=null&&ids.length>0)
        {
            hxpUserecordSearch = new HxpUserecordSearch();
            hxpUserecordSearch.setIds(Arrays.asList(ids));
        }
        List<HxpUserecordVo> list = hxpUserecordService.getHxpUserecordList(hxpUserecordSearch);
        ExcelUtil<HxpUserecordVo> util = new ExcelUtil<HxpUserecordVo>(HxpUserecordVo.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, list, "使用记录", null, "使用记录-导出数据表"+nowDate);
    }

    /**
     * 获取化学品使用记录详细信息
     */
    @ApiOperation(value = "获取化学品使用记录详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<HxpUserecord> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(hxpUserecordService.selectHxpUserecordById(id));
    }

    /**
     * 检测领用超时
     */
    @GetMapping(value = "/checkOvertime")
    public void checkOvertime() {
        hxpUserecordService.checkOvertime();
    }

    /**
     * 定时四医大短信上行查询结果
     */
    @GetMapping(value = "/queryUplink")
    public void queryUplinkResult() {
        smsSydUtil.queryUplinkResult();
    }
}
