package com.chinaweal.aiccs.aiccs.fraudreg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.TFraudCheck;
import com.chinaweal.aiccs.aiccs.fraudreg.service.ITFraudCheckService;
import com.chinaweal.aiccs.aiccs.market.service.ITSUsualInspectService;
import com.chinaweal.aiccs.org.entity.OrgUnits;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
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 chinaweal
 * @since 2022-01-13
 */
@Api(tags = "撤销登记案件转现场核查表")
@ApiSort(1)
@RestController
@RequestMapping("/fraudreg/tFraudCheck")
public class TFraudCheckController {

    @Resource
    private ITFraudCheckService iTFraudCheckService;
    @Resource
    private TUsersService tUsersService;
    @Resource
    private ITSUsualInspectService iTSUsualInspectService;

    @ApiOperation(value = "1.新增撤销登记案件转现场核查表", position = 1)
    @PostMapping("/save")
    public RestResult<TFraudCheck> saveTFraudCheck(@RequestBody @Valid TFraudCheck tFraudCheck, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        tFraudCheck = iTFraudCheckService.saveTFraudCheck(tFraudCheck);
        return RestResult.ok(tFraudCheck);
    }

    @ApiOperation(value = "2.删除撤销登记案件转现场核查表", position = 2)
    @PostMapping("/delete")
    public RestResult<String> removeTFraudCheck(@RequestParam @ApiParam("id") Serializable id) {
        iTFraudCheckService.removeById(id);
        return RestResult.ok();
    }

    @ApiOperation(value = "3.修改撤销登记案件转现场核查表", position = 3)
    @PostMapping("/update")
    public RestResult<String> updateTFraudCheck(@RequestBody @Valid TFraudCheck tFraudCheck, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        iTFraudCheckService.updateTFraudCheck(tFraudCheck);
        return RestResult.ok();
    }

    @ApiOperation(value = "4.查询撤销登记案件转现场核查表", position = 4)
    @GetMapping("/get")
    public RestResult<TFraudCheck> getTFraudCheck(@RequestParam @ApiParam("id") Serializable id) {
        TFraudCheck tFraudCheck = iTFraudCheckService.getById(id);
        return RestResult.ok(tFraudCheck);
    }

    @ApiOperation(value = "5.分页查询撤销登记案件转现场核查表", position = 5)
    @PostMapping("/list")
    public RestResult<IPage<TFraudCheck>> listTFraudCheck(@RequestBody PageRequestDto pageRequestDto) {

        AICUser aicUser = SSOUtil.getUser();
        if (!aicUser.getAicPermissions().containsKey("fraudRegOneAssign")) {
            throw new BusinessException(ResultCode.PERMISSION_NO_ACCESS);
        }

        IPage<TFraudCheck> data = iTFraudCheckService.defaultPaging(pageRequestDto, wrapper -> {
            // 区信用科
            if (aicUser.getRegionID().length() == 6) {
                wrapper.lambda().ne(TFraudCheck::getStatus, "4").eq(TFraudCheck::getDomdistrict, aicUser.getRegionID());
            } else {
                wrapper.lambda().eq(TFraudCheck::getStatus, "1").eq(TFraudCheck::getSendOrg, aicUser.getRegionID());
            }
        }, null);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "6.获取现场核查监管所列表", position = 6)
    @GetMapping("/getSupOrg")
    public RestResult<List<?>> getSupOrg() {
        AICUser aicUser = SSOUtil.getUser();
        List<OrgUnits> orgUnits = tUsersService.byNextLevelPlace(aicUser.getOrgID());
        return RestResult.ok(orgUnits);
    }

    @ApiOperation(value = "7.获取现场核进度", position = 7)
    @GetMapping("/getSteps")
    public RestResult<?> getSteps(@RequestParam String bizSeq) {

        TFraudCheck tFraudCheck = iTFraudCheckService.lambdaQuery().eq(TFraudCheck::getBizSeq, bizSeq).one();

        if (tFraudCheck == null) return RestResult.ok();

        return RestResult.ok(tFraudCheck);
    }

    @ApiOperation(value = "8.批量修改撤销登记案件转现场核查表", position = 3)
    @PostMapping("/batchUpdate")
    public RestResult<String> batchUpdateTFraudCheck(@RequestBody @Valid List<TFraudCheck> tFraudCheckList, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        iTFraudCheckService.batchUpdateTFraudCheck(tFraudCheckList);
        return RestResult.ok();
    }
}
