package com.zd.airbottle.controller;

import cn.hutool.core.collection.CollUtil;
import com.zd.airbottle.domain.QpUseRecord;
import com.zd.airbottle.domain.dto.QpUseRecordDto;
import com.zd.airbottle.domain.vo.QpAirGoodsConfigRelationVo;
import com.zd.airbottle.domain.vo.QpUseRecordVo;
import com.zd.airbottle.domain.vo.report.AirAmount;
import com.zd.airbottle.domain.vo.report.WebUseRecordVo;
import com.zd.airbottle.service.IQpUseRecordService;
import com.zd.common.core.utils.DateUtils;
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 com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用记录Controller
 *
 * @author zd
 */
@RestController
@Api(tags = "【使用记录】")
@RequestMapping("/use/record")
public class QpUseRecordController extends BaseController {
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

    @Autowired
    private IQpUseRecordService qpUseRecordService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询使用记录列表
     */
    @PreAuthorize(hasPermi = "airbottle:use:record:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询使用记录列表")
    public TableDataInfo<QpUseRecordVo> list(QpUseRecord qpUseRecord) {
        startPage("use_time","descending");
        List<QpUseRecord> list = qpUseRecordService.selectQpUseRecordList(qpUseRecord);
        TableDataInfo<QpUseRecord> dataTable = getDataTable(list);
        TableDataInfo<QpUseRecordVo> info = new TableDataInfo<>();
        if (CollUtil.isNotEmpty(list)) {
            BeanUtils.copyProperties(dataTable, info);
            List<QpUseRecordVo> recordVos = qpUseRecordService.getListVo(list);
            info.setRows(recordVos);
        }
        return info;
    }

    /**
     * 信息统计查询使用记录列表
     * @param storageId 库存主ID
     * @return 集合对象
     */
    @GetMapping("/bottle/list/{storageId}")
    @ApiOperation(value = "信息统计查询使用记录列表")
    public List<QpUseRecordVo> bottleList(@PathVariable Long storageId) {
        return qpUseRecordService.getBottleInfoByStorageId(storageId);
    }

    /**
     * 导出使用记录列表
     */
    @ApiOperation(value = "导出使用记录列表")
    @Log(title = "使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpUseRecord qpUseRecord) throws IOException {
        List<QpUseRecord> list = qpUseRecordService.selectQpUseRecordList(qpUseRecord);
        List<QpUseRecordVo> info = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            info = qpUseRecordService.getListVo(list);
            info.forEach(i-> {
                BigDecimal afterUse = i.getAfterUse();
                i.setBeforeUseValue(i.getBeforeUse().stripTrailingZeros().toPlainString()+"MPa");
                if (afterUse!=null){
                    i.setAfterUseValue(afterUse.stripTrailingZeros().toPlainString()+"MPa");
                }
            });
        }
        ExcelUtil<QpUseRecordVo> util = new ExcelUtil<>(QpUseRecordVo.class);
        util.exportExcel(response, info, "使用记录数据" + DateUtils.getDate());
    }

    /**
     * 小程序端获取使用记录详细信息
     */
    @ApiOperation(value = "小程序端获取使用记录详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<QpUseRecordVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpUseRecordService.selectQpUseRecordById(id));
    }

    /**
     * 管理端端获取使用记录详细信息
     */
    @ApiOperation(value = "管理端端获取使用记录详细信息")
    @GetMapping(value = "/bottle/{id}")
    public ResultData<WebUseRecordVo> getBottleInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpUseRecordService.getBottleInfo(id));
    }

    /**
     * 信息统计管理端端获取气瓶使用记录详细信息
     */
    @ApiOperation(value = "信息统计管理端端获取气瓶使用记录详细信息")
    @GetMapping(value = "/bottle/record/{storageId}")
    public ResultData<WebUseRecordVo> getBottleRecord(@PathVariable("storageId") Long storageId) {
        return ResultData.success(qpUseRecordService.getBottleRecord(storageId));
    }

    /**
     * 出入库信息统计
     */
    @ApiOperation(value = "出入库信息统计")
    @GetMapping(value = "/calcRepertory")
    public ResultData<List<AirAmount>> calcRepertory(String airName) {
        return ResultData.success(qpUseRecordService.calcRepertory(airName));
    }

    /**
     * 用气量信息统计
     */
    @ApiOperation(value = "用气量信息统计")
    @GetMapping(value = "/calcAmount")
    public ResultData<List<AirAmount>> calcAmount(String airName) {
        return ResultData.success(qpUseRecordService.calcAmount(airName));
    }

    /**
     * 气瓶申请统计
     */
    @ApiOperation(value = "气瓶申请统计")
    @GetMapping(value = "/calcApply")
    public ResultData<List<AirAmount>> calcApply(String airName) {
        return ResultData.success(qpUseRecordService.calcApply(airName));
    }

    /**
     * 新增使用登记
     * value 说明：
     * 1：正常状态
     * 2：气瓶余量接近安全值
     * 3：用户没有用气资格
     * 4：气瓶正在使用中
     */
    @ApiOperation(value = "新增使用登记")
    @Log(title = "新增使用登记", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Integer> add(@RequestBody QpUseRecordDto recordDto) {
        return ResultData.success(qpUseRecordService.insertQpUseRecord(recordDto));
    }

    /**
     * 归还登记
     */
    @ApiOperation(value = "归还登记")
    @Log(title = "归还登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpUseRecord qpUseRecord) {
        return ResultData.result(qpUseRecordService.updateQpUseRecord(qpUseRecord));
    }

    /***
     * 查询使用记录
     */
    @ApiOperation(value = "查询使用记录")
    @GetMapping(value = "/getUseDetails")
    public TableDataInfo<QpUseRecord> getUseDetails( QpUseRecord qpUseRecord) {
        startPage();
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsList(qpUseRecord);
        return getDataTable(list);
    }

    /**
     * 当前登录人 申请并入库的气瓶使用记录
     */
    @ApiOperation(value = "当前登录人 申请并入库的气瓶使用记录")
    @GetMapping(value = "/getUseDetailsByUserId")
    public TableDataInfo<QpUseRecord> getUseDetailsByUserId(QpUseRecord qpUseRecord) {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsListByUserId(qpUseRecord);
        qpUseRecord.setUserId(sysUser.getUserId());
        startPage();
        return getDataTable(list);
    }

    /***
     * 查询某个气瓶的使用记录汇总
     */
    @ApiOperation(value = "查询某个气瓶的使用记录汇总")
    @GetMapping(value = "/getUseDetailsByStorageId")
    public TableDataInfo<QpUseRecordVo> getUseDetailsByStorageId(QpUseRecordVo qpUseRecordVo) {
        startPage();
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsListByStorageId(qpUseRecordVo);
        return new TableDataInfo<QpUseRecordVo>().getDataTable(list);
    }

    /***
     * 查询使用记录-小程序端
     */
    @ApiOperation(value = "查询使用记录")
    @GetMapping(value = "/getUseDetailsApp")
    public TableDataInfo<QpUseRecord> getUseDetailsApp( QpUseRecord qpUseRecord) {
        startPage();
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsList(qpUseRecord);
        for (QpUseRecordVo useRecordVo:list) {
            useRecordVo.setUseTimeApp(useRecordVo.getUseTime()!=null?sdf.format(useRecordVo.getUseTime()):null);
            useRecordVo.setBackTimeApp(useRecordVo.getBackTime()!=null?sdf.format(useRecordVo.getBackTime()):null);
        }
        return getDataTable(list);
    }

    /**
     * 当前登录人 申请并入库的气瓶使用记录-小程序端
     */
    @ApiOperation(value = "当前登录人 申请并入库的气瓶使用记录-小程序端")
    @GetMapping(value = "/getUseDetailsByUserIdApp")
    public TableDataInfo<QpUseRecord> getUseDetailsByUserIdApp(QpUseRecord qpUseRecord) {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        qpUseRecord.setUserId(sysUser.getUserId());
        startPage();
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsListByUserId(qpUseRecord);
        //时间格式化处理
        for (QpUseRecordVo useRecord: list) {
                useRecord.setUseTimeApp(useRecord.getUseTime()!=null?sdf.format(useRecord.getUseTime()):null);
                useRecord.setBackTimeApp(useRecord.getBackTime()!=null?sdf.format(useRecord.getBackTime()):null);
        }
        return getDataTable(list);
    }

    /**
     * 当前登录人 申请并入库的气瓶使用记录-小程序端
     */
    @ApiOperation(value = "当前登录人 申请并入库的气瓶使用记录-小程序端")
    @GetMapping(value = "/getUseRecordSpecsListApp")
    public TableDataInfo<QpAirGoodsConfigRelationVo> getUseRecordSpecsListApp(QpAirGoodsConfigRelationVo vo) {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        vo.setUserId(sysUser.getUserId());
        startPage();
        List<QpAirGoodsConfigRelationVo> list =  qpUseRecordService.getUseRecordSpecsList(vo);
        for (QpAirGoodsConfigRelationVo useRecord:list) {
            useRecord.setUseTimeApp(useRecord.getUseTime()!=null?sdf.format(useRecord.getUseTime()):null);
            useRecord.setBackTimeApp(useRecord.getBackTime()!=null?sdf.format(useRecord.getBackTime()):null);
        }
        return getDataTable(list);
    }

}
