package com.chushang.inspection.work.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.web.annotation.AutoIdempotent;
import com.chushang.inspection.work.dto.BankDispatchQuery;
import com.chushang.inspection.work.dto.WrkIcbcJsReceive;
import com.chushang.inspection.work.po.WrkIcbcJs;
import com.chushang.inspection.work.service.WrkIcbcJsService;
import com.chushang.inspection.work.vo.BankDispatchDTO;
import com.chushang.inspection.work.vo.Result;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
* 江苏工行数据推送(wrk_icbc_js)表控制层
*
* @author xxxxx
*/
@Slf4j
@RestController
@RequestMapping("/icbc/js")
public class WrkIcbcJsController {

    @Resource
    private WrkIcbcJsService wrkIcbcJsService;

    @SysLog(value = "江苏工行数据接收", remote = true, businessType = BusinessType.INSERT)
    @PostMapping("/receive")
    public Result receive(@RequestBody WrkIcbcJsReceive dto) {
        try {
            Assert.notNull(dto, "接收数据为空");
            wrkIcbcJsService.receive(dto);
            return Result.success();
        } catch (Exception e) {
            log.error("数据接受失败", e);
            return Result.error();
        }
    }

    /**
     * 查询银行派单列表
     */
    @SysLog(value = "查询银行派单列表", businessType = BusinessType.QUERY)
    @GetMapping("/page")
    @RequiresPermissions("icbc:js:page")
    public AjaxResult queryDispatchPage(@Validated BankDispatchQuery query) {
        return AjaxResult.success(wrkIcbcJsService.queryDispatchPage(query));
    }

    /**
     * 查询银行派单详情
     */
    @SysLog(value = "查询银行派单详情", businessType = BusinessType.QUERY)
    @GetMapping("/info")
    @RequiresPermissions("icbc:js:info")
    public AjaxResult info(@RequestParam("id") Long id) {
        return AjaxResult.success(wrkIcbcJsService.info(id));
    }

    /**
     * 银行派单数据更新
     */
    @AutoIdempotent
    @SysLog(value = "银行派单数据更新", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @RequiresPermissions("icbc:js:update")
    public void update(@RequestBody @Validated BankDispatchDTO dispatch) {
        wrkIcbcJsService.update(dispatch);
    }

    /**
     * 手工派单
     */
    @AutoIdempotent
    @SysLog(value = "手工派单", businessType = BusinessType.INSERT)
    @PostMapping("/dispatch")
    @RequiresPermissions("icbc:js:dispatch")
    public void dispatchOrder(@RequestBody Map<String, Long> data) {
        Assert.isTrue(CollUtil.isNotEmpty(data) && data.containsKey("id") && data.containsKey("userId"),
                "id或用户id不能位空");
        WrkIcbcJs entity = new WrkIcbcJs();
        entity.setId(data.get("id"));
        wrkIcbcJsService.dispatchOrder(entity, data.get("userId"));
    }

    /**
     * 自动推送失败手动推送
     */
    @AutoIdempotent
    @SysLog(value = "自动推送失败手动推送", businessType = BusinessType.OTHER)
    @PostMapping("/push")
    @RequiresPermissions("icbc:js:push")
    public void push(@RequestBody Map<String, Long> data) {
        Assert.isTrue(data != null && data.containsKey("id"), "id不能位空");
        wrkIcbcJsService.manualPush(data.get("id"));
    }

}
