package com.brilliance.isc.common.lock.controller;

import com.brilliance.isc.bo.Lck;
import com.brilliance.isc.bo.Spt;
import com.brilliance.isc.common.component.SptDataComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.lock.BizLocker;
import com.brilliance.isc.common.lock.LockInfo;
import com.brilliance.isc.common.lock.common.BizCommonLocker;
import com.brilliance.isc.common.trnmod.service.PendingCheckService;
import com.brilliance.isc.constant.ErrorCode;
import com.brilliance.isc.mda.dao.LckMapper;
import com.brilliance.isc.mda.dao.SptMapper;
import com.brilliance.isc.vo.PendingCheckResult;
import com.brilliance.isc.vo.PendingCheckVo;
import com.brilliance.isc.vo.ResponseSet;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lock")
public class LockController {

    @Autowired
    private BizLocker bizLocker;

    @Autowired
    private BizCommonLocker bizCommonLocker;

    @Autowired
    private SptMapper sptMapper;

    @Autowired
    private LckMapper lckMapper;

    @Autowired
    private PendingCheckService pendingCheckService;

    @Autowired
    private SptDataComponent sptDataComponent;

    @PostMapping("/isAccessible")
    public ResponseSet<LockInfo> accessible(LockInfo lockInfo){
        lockInfo.setUsrId(SettleContext.getSessionUserVo().getLogName());
        if(!bizLocker.isAccessible(lockInfo)){
            LockInfo respLock = bizLocker.getLock(lockInfo);
            if(respLock != null) {
                respLock.setTrnLabel(bizCommonLocker.convertTransName(respLock.getTrnName()));
            }
            return new ResponseSet(ErrorCode.LOCK_BIZ_FAIL,"无法锁定",respLock);
        }
        return ResponseSet.simpleSuccess("可以锁定");
    }

    @PostMapping("/isReroutingAccessible")
    public ResponseSet<LockInfo> isReroutingAccessible(LockInfo lockInfo){
        lockInfo.setUsrId(SettleContext.getSessionUserVo().getLogName());
        if(!bizLocker.isAccessible(lockInfo,"Y")){
            LockInfo respLock = bizLocker.getLock(lockInfo);
            if(respLock != null) {
                respLock.setTrnLabel(bizCommonLocker.convertTransName(respLock.getTrnName()));
            }
            return new ResponseSet(ErrorCode.LOCK_BIZ_FAIL,"无法锁定",respLock);
        }
        return ResponseSet.simpleSuccess("可以锁定");
    }

    /**
     * 押汇中心复核批量分配任务时获取锁信息
     * @param map
     * @return
     */
    @PostMapping("/isAssignRerouting")
    public ResponseSet<List<Lck>> isAssignRerouting(@RequestBody Map<String, Object> map){
        List<String> lckstrList=(List)map.get("lckstrList");
        List<Lck> lckList=lckMapper.selectByLckstr(lckstrList);
        return ResponseSet.simpleSuccess(lckList);
    }


    @PostMapping("/isSptAccessible")
    public ResponseSet<LockInfo> sptAccessible(LockInfo lockInfo){
        lockInfo.setUsrId(SettleContext.getSessionUserVo().getLogName());
        if(!bizLocker.isAccessible(lockInfo)){
            LockInfo respLock = bizLocker.getLock(lockInfo);
            if(respLock != null) {
                respLock.setTrnLabel(bizCommonLocker.convertTransName(respLock.getTrnName()));
            }
            return new ResponseSet(ErrorCode.LOCK_BIZ_FAIL,"待办任务无法锁定",respLock);
        }
        Spt spt = new Spt();
        spt.setInr(lockInfo.getObjInr());
        spt = sptMapper.selectByPrimaryKey(spt);

        lockInfo.setObjInr(spt.getObjinr());
        lockInfo.setObjType(spt.getObjtyp());
        //如果是主表数据存在；或者不存在但是为起头交易，才允许继续做
        //只考虑Inr存在且为暂存和打回的情况
        if(!MdaUtils.isEmpty(spt.getObjinr()) && "PEN-COR".indexOf(spt.getSta())>-1) {
            boolean isSptCanEnter = bizCommonLocker.isAccess(spt.getObjtyp(), spt.getObjinr(), spt.getOrdinr());
            if (!isSptCanEnter) {
                lockInfo.setTrnLabel(bizCommonLocker.convertTransName(lockInfo.getTrnName()));
                return new ResponseSet(ErrorCode.LOCK_BIZ_WITHOUT_BEGIN, "待办任务无法锁定", lockInfo);
            }
        }
        if(!bizLocker.isAccessible(lockInfo)){
            LockInfo respLock = bizLocker.getLock(lockInfo);
            if(respLock != null) {
                respLock.setTrnLabel(bizCommonLocker.convertTransName(respLock.getTrnName()));
            }
            return new ResponseSet(ErrorCode.LOCK_BIZ_FAIL,"业务数据无法锁定",respLock);
        }
        return ResponseSet.simpleSuccess("可以锁定");
    }

    @PostMapping("/getSptAccessible")
    public ResponseSet<LockInfo> getSptAccessible(LockInfo lockInfo){
        lockInfo.setUsrId(SettleContext.getSessionUserVo().getLogName());
        LockInfo respLock = bizLocker.getLock(lockInfo);
        if(respLock != null) {
            respLock.setTrnLabel(bizCommonLocker.convertTransName(respLock.getTrnName()));
        }
        return ResponseSet.simpleSuccess(respLock);
    }


    @PostMapping("/checkPending")
    public ResponseSet<PendingCheckResult> checkPending(PendingCheckVo pendingCheckVo){
        pendingCheckVo.setSetWarning(false);
        PendingCheckResult pendingCheckResult = pendingCheckService.checkPendingSPTs(pendingCheckVo);
        return ResponseSet.simpleSuccess(pendingCheckResult);
    }


}
