package com.ruoyi.web.controller.pub;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Assetlog;
import com.ruoyi.system.domain.vo.AssetVo;
import com.ruoyi.system.service.AssetlogService;
import com.ruoyi.system.service.DepartmentService;
import com.ruoyi.web.controller.tool.RSAUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 资产
 * @author: elis
 * @date: 2024.05.27
 */
@Anonymous
@RestController
@RequestMapping("/system/dept")
public class AssetController extends BaseController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private AssetlogService assetlogService;

    @GetMapping("/getDepartments")
    public AjaxResult getDepartments(String departmentName) {
        return success(departmentService.getDepartments(departmentName));
    }

    @GetMapping("/getDepartmentsV2")
    public AjaxResult getDepartmentsV2(String departmentName, String goodsName) {
        return success(departmentService.getDepartmentsV2(departmentName, goodsName));
    }

    @GetMapping("/sumDepartmentAssetNum")
    public AjaxResult sumDepartmentAssetNum(Integer departmentId, String goodsName, String assetType) {
        return success(departmentService.sumDepartmentAssetNum(departmentId, goodsName, assetType));
    }

    @GetMapping("/getAsset")
    public AjaxResult getAsset(Integer id) {
        return success(assetlogService.queryById(id));
    }

    @GetMapping("/assetList")
    public TableDataInfo list(AssetVo assetVo) {
        startPage();
        List<Assetlog> list = assetlogService.list(assetVo);
        return getDataTable(list);
    }

    @GetMapping("/getAssetType")
    public AjaxResult getAssetType(Integer departmentId, Integer type) {
        return success(assetlogService.getAssetType(departmentId, type));
    }

    @GetMapping("/getAssetSource")
    public AjaxResult getAssetSource(Integer departmentId) {
        return success(assetlogService.getAssetSource(departmentId));
    }

    @GetMapping("/getAssetRecord")
    public AjaxResult getAssetRecord(Integer assetId) {
        Map<String, Object> result = new HashMap<>();
        result.put("borrow", assetlogService.getAssetBorrow(assetId));
        result.put("repair", assetlogService.getAssetRepair(assetId));
        result.put("scrap", assetlogService.getAssetScrap(assetId));
        result.put("giveBack", assetlogService.getAssetGiveBack(assetId));
        return success(result);
    }

    @GetMapping("/mobileLogin")
    public AjaxResult mobileLogin(String username, String password) {
        return success(assetlogService.mobileLogin(username, password));
    }

    @GetMapping("/createQRCode")
    public AjaxResult createQRCode(String url, Integer days) {
        if (StrUtil.isEmpty(url) || days == null) {
            return success();
        }
        try {
            url += "?edata=";
            String dataVal = "";
            String msg = "";
            if (days == -1) {
                dataVal = Base64.encode("any");
                msg = "永久有效";
            } else {
                Date endDate = DateUtil.offsetDay(DateUtil.date(), days);
                String date = DateUtil.format(endDate, "yyyy-MM-dd");
                msg = "有效期至：" + date;
                dataVal = Base64.encode(date);
            }
            System.out.println("dataVal==>" + dataVal);
            String val = RSAUtils.encrypt(dataVal);
            url += Base64.encode(val);
            System.out.println("url==>" + url);
            byte[] qrcode = QrCodeUtil.generatePng(url, 300, 300);
            String base64 = Base64.encode(qrcode);
            return AjaxResult.success(msg, base64);
        } catch (Exception e) {
            return AjaxResult.success("生成二维码失败");
        }
    }

    @GetMapping("/verifyQRCodeData")
    public AjaxResult verifyQRCodeData(String val) {
        try {
            val = Base64.decodeStr(val);
            System.out.println("val==>" + val);
            String date = RSAUtils.decrypt(val);
            date = Base64.decodeStr(date);
            if ("any".equals(date)) {
                return success(true);
            }
            System.out.println("date==>" + DateUtil.parse(date));
            int result = DateUtil.compare(DateUtil.parse(date), DateUtil.parse(DateUtil.today()));
            System.out.println("result==>" + result);
            return success(result > -1);
        } catch (Exception e) {
            return success(false);
        }
    }

}
