package com.xjrsoft.module.textbook.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.QrCodeUtil;
import com.xjrsoft.config.CommonPropertiesConfig;
import com.xjrsoft.module.textbook.dto.AddWfTextbookClaimDto;
import com.xjrsoft.module.textbook.dto.ConfirmDistributeDto;
import com.xjrsoft.module.textbook.dto.UpdateWfTextbookClaimDto;
import com.xjrsoft.module.textbook.dto.WfTextbookClaimPageDto;
import com.xjrsoft.module.textbook.entity.WfTextbookClaim;
import com.xjrsoft.module.textbook.service.IWfTextbookClaimService;
import com.xjrsoft.module.textbook.vo.WfTextbookClaimPageVo;
import com.xjrsoft.module.textbook.vo.WfTextbookClaimVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 教材申领
* @Author szs
* @Date: 2023-12-26
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/wfTextbookClaim")
@Api(value = "/textbook"  + "/wfTextbookClaim",tags = "教材申领代码")
@AllArgsConstructor
public class WfTextbookClaimController {


    private final IWfTextbookClaimService wfTextbookClaimService;

    private final CommonPropertiesConfig commonPropertiesConfig;

    @GetMapping(value = "/page")
    @ApiOperation(value="教材申领列表(分页)")
    @SaCheckPermission("wftextbookclaim:detail")
    public RT<PageOutput<WfTextbookClaimPageVo>> page(@Valid WfTextbookClaimPageDto dto){

        IPage<WfTextbookClaimPageVo> page = wfTextbookClaimService.getPage(dto);
        PageOutput<WfTextbookClaimPageVo> pageOutput = ConventPage.getPageOutput(page, WfTextbookClaimPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教材申领信息")
    @SaCheckPermission("wftextbookclaim:detail")
    public RT<WfTextbookClaimVo> info(@RequestParam Long id){
        WfTextbookClaim wfTextbookClaim = wfTextbookClaimService.getByIdDeep(id);
        if (wfTextbookClaim == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfTextbookClaim, WfTextbookClaimVo.class));
    }

    @GetMapping(value = "/info-distribute")
    @ApiOperation(value="教材发放页详情")
    @SaCheckPermission("wftextbookclaim:detail")
    public RT<WfTextbookClaimVo> infoDistribute(@RequestParam Long id){
        WfTextbookClaimVo wfTextbookClaimVo = wfTextbookClaimService.infoDistribute(id);
        if (wfTextbookClaimVo == null) {
            return RT.error("没有发放数据！");
        }
        return RT.ok(BeanUtil.toBean(wfTextbookClaimVo, WfTextbookClaimVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教材申领")
    @SaCheckPermission("wftextbookclaim:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfTextbookClaimDto dto){
        WfTextbookClaim wfTextbookClaim = BeanUtil.toBean(dto, WfTextbookClaim.class);
        boolean isSuccess = wfTextbookClaimService.add(wfTextbookClaim);
    return RT.ok(isSuccess);
    }

    @PutMapping("/confirm-istribute")
    @ApiOperation(value = "确认发放")
    @SaCheckPermission("wftextbookclaim:edit")
    public RT<Boolean> confirmDistribute(@Valid @RequestBody ConfirmDistributeDto dto){
        return RT.ok(wfTextbookClaimService.confirmDistribute(dto));
    }

    @PutMapping
    @ApiOperation(value = "修改教材申领")
    @SaCheckPermission("wftextbookclaim:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfTextbookClaimDto dto){

        WfTextbookClaim wfTextbookClaim = BeanUtil.toBean(dto, WfTextbookClaim.class);
        return RT.ok(wfTextbookClaimService.update(wfTextbookClaim));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教材申领")
    @SaCheckPermission("wftextbookclaim:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfTextbookClaimService.delete(ids));

    }

    @GetMapping(value = "/info-qrcode")
    @ApiOperation(value="教材领取-生成二维码")
    @SaCheckPermission("wfstudenttextbookclaim:detail")
    public RT<String> qrcode(@RequestParam Long id) throws Exception {
        String url = commonPropertiesConfig.getDomainApp() + "/pages/material/grant?id=" + id + "&userId=" + StpUtil.getLoginIdAsLong();
        int width = 200;
        int height = 200;
        int margin = 1;

        try {
            String base64 = QrCodeUtil.createBase64(url, width, height, margin);
            return RT.ok(base64);
        } catch (Exception e) {
            throw e;
        }

    }

}