package com.zd.chemical.controller;

import com.zd.chemical.domain.HxpStock;
import com.zd.chemical.domain.vo.HxpStockDetailVO;
import com.zd.chemical.domain.vo.HxpStockJoinVO;
import com.zd.chemical.domain.vo.HxpStockOutVO;
import com.zd.chemical.domain.vo.HxpStockSearch;
import com.zd.chemical.service.IHxpStockService;
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.SecurityUtils;
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.InventoryTag;
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 javax.validation.constraints.NotNull;
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-17
 */
@RestController
@Api(tags = "【库存管理】")
@RequestMapping("/hxpStock")
public class HxpStockController extends BaseController {
    @Autowired
    private IHxpStockService hxpStockService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SmsSydUtil smsSydUtil;

    /**
     * 查询库存管理列表
     */
    @PreAuthorize(hasPermi = "chemical:hxpStock:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询库存管理列表")
    public TableDataInfo<HxpStock> list(HxpStock hxpStock) {
        startPage();
        List<HxpStock> list = hxpStockService.selectHxpStockList(hxpStock);
        return getDataTable(list);
    }

    /**
     * 导出库存管理列表
     */
    @ApiOperation(value = "导出库存管理列表")
    @PreAuthorize(hasPermi = "chemical:hxpStock:export")
    @Log(title = "库存管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HxpStock hxpStock) throws IOException {
        List<HxpStock> list = hxpStockService.selectHxpStockList(hxpStock);
        ExcelUtil<HxpStock> util = new ExcelUtil<HxpStock>(HxpStock.class);
        util.exportExcel(response, list, "库存管理数据");
    }

    /**
     * 查询库存管理列表(库存明细)
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCKDETAIL+ PerFun.LIST)
    @GetMapping("/listDetail")
    @ApiOperation(value = "查询库存管理列表(库存明细)")
    public TableDataInfo<HxpStockDetailVO> listDetail(HxpStockSearch hxpStockSearch) {
        startPage();
        List<HxpStockDetailVO> list = hxpStockService.selectHxpStockListDetail(hxpStockSearch);
        return getDataTable(list);
    }

    /**
     * 导出化学品信息列表
     */
    @ApiOperation(value = "导出化学品信息列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCK+ PerFun.DETAILEXPORT)
    @Log(title = "化学品信息", businessType = BusinessType.EXPORT)
    @PostMapping({"/exportDetail","/exportDetail/{ids}"})
    public void exportDetail(HttpServletResponse response, HxpStockSearch hxpStockSearch, @PathVariable(value = "ids",required = false) Long... ids) throws IOException {
        if(ids!=null&&ids.length>0)
        {
            hxpStockSearch = new HxpStockSearch();
            hxpStockSearch.setIds(Arrays.asList(ids));
        }
        List<HxpStockDetailVO> list = hxpStockService.selectHxpStockListDetail(hxpStockSearch);
        ExcelUtil<HxpStockDetailVO> util = new ExcelUtil<HxpStockDetailVO>(HxpStockDetailVO.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, list, "化学品库存明细", null, "化学品库存明细-导出数据表"+nowDate);
    }

    /**
     * 查询库存管理列表(入库记录)
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCKJOIN+ PerFun.LIST)
    @GetMapping("/listJoin")
    @ApiOperation(value = "查询库存管理列表(入库记录)")
    public TableDataInfo<HxpStockJoinVO> listJoin(HxpStockSearch hxpStockSearch) {
        startPage();
        List<HxpStockJoinVO> list = hxpStockService.selectHxpStockListJoin(hxpStockSearch);
        return getDataTable(list);
    }

    @GetMapping("/listJoinByUser")
    @ApiOperation(value = "学生端查询入库记录")
    public TableDataInfo<HxpStockJoinVO> listJoinByUser(HxpStockSearch hxpStockSearch) {
        startPage();
        hxpStockSearch.setLoginUserId(SecurityUtils.getUserId());
        List<HxpStockJoinVO> list = hxpStockService.selectHxpStockListJoin(hxpStockSearch);
        return getDataTable(list);
    }


    /**
     * 导出化学品信息列表(入库记录)
     */
    @ApiOperation(value = "导出化学品信息列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCK+ PerFun.JOINEXPORT)
    @Log(title = "化学品信息", businessType = BusinessType.EXPORT)
    @PostMapping({"/exportJoin","/exportJoin/{ids}"})
    public void exportJoin(HttpServletResponse response, HxpStockSearch hxpStockSearch, @PathVariable(value = "ids",required = false) Long... ids) throws IOException {
        if(ids!=null&&ids.length>0)
        {
            hxpStockSearch = new HxpStockSearch();
            hxpStockSearch.setIds(Arrays.asList(ids));
        }
        List<HxpStockJoinVO> list = hxpStockService.selectHxpStockListJoin(hxpStockSearch);
        ExcelUtil<HxpStockJoinVO> util = new ExcelUtil<HxpStockJoinVO>(HxpStockJoinVO.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, list, "化学品入库记录", null, "化学品入库记录-导出数据表"+nowDate);
    }

    /**
     * 查询库存管理列表(出库记录)
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCKOUT+ PerFun.LIST)
    @GetMapping("/listOut")
    @ApiOperation(value = "查询库存管理列表(出库记录)")
    public TableDataInfo<HxpStockOutVO> listOut(HxpStockSearch hxpStockSearch) {
        startPage();
        List<HxpStockOutVO> list = new ArrayList<>();
        if(tokenService.getLoginUser().getSysUser().getUserType().equals("22")) {
            hxpStockSearch.setLoginUserId(tokenService.getLoginUser().getUserid());
            list = hxpStockService.selectHxpStockListOut(hxpStockSearch);
        }else{
            list = hxpStockService.selectHxpStockListOut(hxpStockSearch);
        }
        return getDataTable(list);
    }


    @GetMapping("/listOutByUser")
    @ApiOperation(value = "学生查询库存管理列表(出库记录)")
    public TableDataInfo<HxpStockJoinVO> listOutByUser(HxpStockSearch hxpStockSearch) {
        startPage();
        hxpStockSearch.setLoginUserId(SecurityUtils.getUserId());
        List<HxpStockOutVO> list = hxpStockService.selectHxpStockListOut(hxpStockSearch);
        return getDataTable(list);
    }

    /**
     * 导出化学品信息列表(出库记录)
     */
    @ApiOperation(value = "导出化学品信息列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_STOCK+ PerFun.OUTEXPORT)
    @Log(title = "化学品信息", businessType = BusinessType.EXPORT)
    @PostMapping({"/exportOut","/exportOut/{ids}"})
    public void exportOut(HttpServletResponse response, HxpStockSearch hxpStockSearch, @PathVariable(value = "ids",required = false) Long... ids) throws IOException {
        if(ids!=null&&ids.length>0)
        {
            hxpStockSearch = new HxpStockSearch();
            hxpStockSearch.setIds(Arrays.asList(ids));
        }
        List<HxpStockOutVO> list = hxpStockService.selectHxpStockListOut(hxpStockSearch);
        ExcelUtil<HxpStockOutVO> util = new ExcelUtil<HxpStockOutVO>(HxpStockOutVO.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, list, "化学品出库记录", null, "化学品出库记录-导出数据表"+nowDate);
    }

    /**
     * 获取库存管理详细信息
     */
    @ApiOperation(value = "获取库存管理详细信息")
    @PreAuthorize(hasPermi = "chemical:hxpStock:query")
    @GetMapping(value = "/{id}")
    public ResultData<HxpStock> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(hxpStockService.selectHxpStockById(id));
    }


    @ApiOperation(value = "RFID违规检测")
    @PostMapping(value = "/RFIDCheck")
    public R<Boolean> RFIDCheck(@RequestBody InventoryTag tag) {
        return R.ok(hxpStockService.RFIDCheck(tag));
    }

    /**
     * 化学品库存过期检测
     */
    @GetMapping(value = "/expireCheck")
    public void expireCheck() {
        hxpStockService.expireCheck();
    }

    /**
     * 四医大平台发送短信
     * @param content 发送内容
     * @param purpose 短信用途 1 化学品告警，2 登录，3 开锁，4 气瓶告警，5 预案告警
     * @param phones  手机号
     * @return 发送结果
     */
    @GetMapping(value = "/sendSydSms")
    public R sendSydSms(@RequestParam(value = "content") String content,
                        @RequestParam(value = "purpose") Integer purpose,
                        @RequestParam(value = "lockApplyId", required = false) Long lockApplyId,
                        @RequestParam(value = "phones") @NotNull String... phones){

        try {
            return smsSydUtil.sendSydSms(content, purpose, lockApplyId, phones);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("四医大短信调用发生异常!" + e.getMessage());
           return R.fail("短信发送失败：" + e.getMessage());
        }
    }
}
