package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.risk.service.RiskValidator;
import com.ruoyi.ss.riskInfo.domain.RiskInfo;
import com.ruoyi.ss.riskInfo.domain.RiskInfoQuery;
import com.ruoyi.ss.riskInfo.domain.RiskInfoVO;
import com.ruoyi.ss.riskInfo.domain.dto.RiskInfoSubmitDTO;
import com.ruoyi.ss.riskInfo.domain.enums.RiskInfoStatus;
import com.ruoyi.ss.riskInfo.service.RiskInfoConverter;
import com.ruoyi.ss.riskInfo.service.RiskInfoService;
import com.ruoyi.ss.riskInfo.service.RiskInfoValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author wjh
 * 2024/11/25
 */
@RestController
@RequestMapping("/app/riskInfo")
public class AppRiskInfoController extends BaseController {

    @Autowired
    private RiskInfoService riskInfoService;

    @Autowired
    private RiskInfoConverter riskInfoConverter;

    @Autowired
    private RiskValidator riskValidator;

    @Autowired
    private RiskInfoValidator riskInfoValidator;

    @ApiOperation("新增风控材料")
    @PostMapping
    public AjaxResult save(@RequestBody @Validated(ValidGroup.FrontCreate.class) RiskInfoSubmitDTO dto) {
        if (!riskValidator.isUser(dto.getRiskId(), getUserId())) {
            return error("您无权提交该风控信息");
        }

        RiskInfo po = riskInfoConverter.toPoByAppCreate(dto);
        int insert = riskInfoService.insertRiskInfo(po);
        ServiceUtil.assertion(insert != 1, "数据保存失败");

        if (dto.getIsSubmit() != null && dto.getIsSubmit()) {
            int submit = riskInfoService.submit(po.getInfoId());
            ServiceUtil.assertion(submit != 1, "数据提交失败");
        }

        return success(po.getInfoId());
    }

    @ApiOperation("修改风控材料")
    @PutMapping
    public AjaxResult update(@RequestBody @Validated(ValidGroup.FrontUpdate.class) RiskInfoSubmitDTO dto) {
        if (!riskInfoValidator.isUser(dto.getInfoId(), getUserId())) {
            return error("您无权修改该风控材料");
        }
        if (!riskValidator.isUser(dto.getRiskId(), getUserId())) {
            return error("您无权修改该风控信息");
        }

        RiskInfo po = riskInfoConverter.toPoByAppUpdate(dto);
        int update = riskInfoService.updateRiskInfo(po);
        ServiceUtil.assertion(update != 1, "数据保存失败");

        if (dto.getIsSubmit() != null && dto.getIsSubmit()) {
            int submit = riskInfoService.submit(po.getInfoId());
            ServiceUtil.assertion(submit != 1, "数据提交失败");
        }

        return success(po.getInfoId());
    }

    @ApiOperation("查询风控材料详情")
    @GetMapping("/{infoId}")
    public AjaxResult getDetail(@PathVariable Long infoId) {
        RiskInfoVO riskInfo = riskInfoService.selectRiskInfoByInfoId(infoId);
        if (!riskValidator.isUser(riskInfo.getRiskId(), getUserId())) {
            return error("您无权查看该风控信息");
        }
        return success(riskInfo);
    }

    @ApiOperation("查询本人风控材料列表")
    @GetMapping("/list")
    public TableDataInfo getList(RiskInfoQuery query) {
        startPage();
        query.setUserId(getUserId());
        return getDataTable(riskInfoService.selectRiskInfoList(query));
    }

}
