package com.bonus.material.materialStation.controller;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.biz.domain.lease.LeaseApplyInfo;
import com.bonus.common.biz.domain.lease.LeasePublishInfo;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.materialStation.domain.ProAuthorizeDetails;
import com.bonus.material.materialStation.domain.ProAuthorizeInfo;
import com.bonus.material.materialStation.service.ProAuthorizeService;
import com.bonus.material.repair.domain.vo.RepairDeviceVO;
import com.bonus.system.api.RemoteFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author hay
 * @description 项目部授权
 * @date 2025/6/18 21:54
 */
@Api(tags = " 项目部授权")
@RestController
@RequestMapping("/authorize")
public class ProAuthorizeController extends BaseController {

    @Resource(name = "proAuthorizeService")
    private ProAuthorizeService service;

    @Resource
    private RemoteFileService sysFileService;

    /**
     * 列表展示
     */
    @ApiOperation(value = "列表展示")
    @GetMapping("/list")
    public AjaxResult getPublishList(LeaseApplyInfo leaseApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        try {
            List<LeasePublishInfo> list = service.getPublishList(leaseApplyInfo);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
        } catch (Exception e) {
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, new ArrayList<>()));
        }
    }

    /**
     * 上传与识别身份证文件
     */
    @ApiOperation(value = "上传与识别身份证文件")
    @PostMapping("/uploadPhoto")
    public AjaxResult uploadPhoto(MultipartFile file,Long type) {
        try {
            AjaxResult result = sysFileService.upload(file);
            if (!result.isSuccess()){
                return AjaxResult.error("文件上传失败");
            }
            Map<String, Object> jsonObject = (Map<String, Object>) result.get("data");
            String fileName = file.getOriginalFilename();
            String filePath = jsonObject.get("url").toString();
            return service.uploadPhoto(filePath,type);
        } catch (Exception e) {
            logger.error("上传退料单PDF文件失败", e);
            return AjaxResult.error("领料单档案保存失败：" + e.getMessage());
        }
    }

    /**
     * 授权提交
     */
    @ApiOperation(value = "授权提交")
    @PostMapping("/authorizeSubmit")
    public AjaxResult authorizeSubmit(@RequestBody ProAuthorizeInfo bean) {
        return service.authorizeSubmit(bean);
    }

    /**
     * 授权信息查看
     */
    @ApiOperation(value = "授权信息查看")
    @PostMapping("/authorizeView")
    public AjaxResult authorizeView(@RequestBody ProAuthorizeInfo bean) {
        return service.authorizeView(bean);
    }

    /**
     * 授权委托书查看
     */
    @ApiOperation(value = "授权委托书查看")
    @PostMapping("/authorizeInfoView")
    public AjaxResult authorizeInfoView(@RequestBody ProAuthorizeInfo bean) {
        return service.authorizeInfoView(bean);
    }

    /**
     * 授权书电子签名
     */
    @ApiOperation(value = "授权书电子签名")
    @PreventRepeatSubmit
    @SysLog(title = "授权书电子签名", businessType = OperaType.UPDATE, logType = 1,module = "授权管理->授权书电子签名")
    @PostMapping("/updateLeaseApplyInfoSign")
    public AjaxResult updateAuthorizeInfoSign(@RequestBody @NotNull ProAuthorizeDetails bean) {
        try {
            return toAjax(service.updateAuthorizeInfoSign(bean));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 列表展示
     */
    @ApiOperation(value = "列表展示")
    @GetMapping("/getAuthorList")
    public AjaxResult getAuthorList(ProAuthorizeInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        try {
            List<ProAuthorizeInfo> list = service.getAuthorList(bean);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
        } catch (Exception e) {
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, new ArrayList<>()));
        }
    }
}

