package com.chinaweal.aiccs.aiccs.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.business.entity.XrOpinion;
import com.chinaweal.aiccs.aiccs.business.entity.XrTaskList;
import com.chinaweal.aiccs.aiccs.business.service.IXrOpinionService;
import com.chinaweal.aiccs.aiccs.business.service.IXrTaskListService;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 监管共用审批意见表 控制器
 * </p>
 *
 * @author quebh
 * @since 2023-05-06
 */
@Api(tags = "4.监管共用审批意见表")
@ApiSort(4)
@RestController
@RequestMapping("/xrOpinion")
public class XrOpinionController {

    @Resource
    private IXrOpinionService iXrOpinionService;

    @Resource
    private IXrTaskListService iXrTaskListService;

    @ApiOperation(value = "1.新增监管共用审批意见表", position = 1)
    @PostMapping("/save")
    public RestResult<XrOpinion> saveXrOpinion(@RequestBody @Valid XrOpinion xrOpinion, BindingResult bindingResult) {
//        AssertUtils.isBindingValid(bindingResult);
        xrOpinion = iXrOpinionService.saveXrOpinion(xrOpinion);
        return RestResult.ok(xrOpinion);
    }

    @ApiOperation(value = "2.删除监管共用审批意见表", position = 2)
    @PostMapping("/delete")
    public RestResult<String> removeXrOpinion(@RequestParam @ApiParam("id") Serializable id) {
        iXrOpinionService.removeById(id);
        return RestResult.ok();
    }

    @ApiOperation(value = "3.修改监管共用审批意见表", position = 3)
    @PostMapping("/update")
    public RestResult<String> updateXrOpinion(@RequestBody @Valid XrOpinion xrOpinion, BindingResult bindingResult) {
//        AssertUtils.isBindingValid(bindingResult);
        iXrOpinionService.updateById(xrOpinion);
        return RestResult.ok();
    }

    @ApiOperation(value = "4.查询监管共用审批意见表", position = 4)
    @GetMapping("/get")
    public RestResult<XrOpinion> getXrOpinion(@RequestParam @ApiParam("id") Serializable id) {
        XrOpinion xrOpinion = iXrOpinionService.getById(id);
        return RestResult.ok(xrOpinion);
    }

    @ApiOperation(value = "5.分页查询监管共用审批意见表", position = 5)
    @PostMapping("/list")
    public RestResult<IPage<XrOpinion>> listXrOpinion(@RequestBody BaseQuery<XrOpinion> query) {
        IPage<XrOpinion> data = iXrOpinionService.listXrOpinion(query);
        return RestResult.ok(data);
    }

    /**
     * @param bizSeqNo 业务号
     * @return
     */
    @ApiOperation(value = "6.获取业务意见", position = 6)
    @PostMapping("/listByBizSeqNo")
    public RestResult<List<XrOpinion>> listByBizSpeNo(@ApiParam("业务号") @RequestParam String bizSeqNo) {
        LambdaQueryWrapper<XrTaskList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XrTaskList::getBizSeqNo, bizSeqNo);
        XrTaskList one = iXrTaskListService.getOne(queryWrapper);
        if (one == null) {
            return RestResult.error(ResultCode.DATA_NONE);
        }
        LambdaQueryWrapper<XrOpinion> query = new LambdaQueryWrapper<>();
        query.eq(XrOpinion::getTaskListId, one.getTaskLikeId())
                .orderByDesc(XrOpinion::getUpdateTime);
        List<XrOpinion> data = iXrOpinionService.list(query);
        return RestResult.ok(data);
    }

}
