package com.ssj.service.kmt.kmt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ssj.bean.kmt.kmt.dto.KmtSysUserAuthDto;
import com.ssj.bean.kmt.kmt.request.exercise.KmtQuestionOperaterRequest;
import com.ssj.bean.kmt.kmtsys.domain.KmtSysUser;
import com.ssj.dao.kmt.kmt.dao.KmtQueryDao;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.kmt.dto.KnowledgeBaseDto;
import com.ssj.service.kmt.service.KmtLoginAuthService;
import com.ssj.service.kmt.service.KmtSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class KmtLoginAuthServiceImpl implements KmtLoginAuthService {

    @Autowired
    private KmtSysUserService kmtSysUserService;
    @Autowired
    private KmtQueryDao queryDao;

    @Override
    public List<KnowledgeBaseDto> getQuestionOperateType() {
        List<KmtSysUserAuthDto> list = JSONArray.parseArray( PropertiesUtil.getValue("KMT_SYS_USER_AUTH"), KmtSysUserAuthDto.class);
        List<KnowledgeBaseDto> result = new ArrayList<>();
        for (KmtSysUserAuthDto kmtSysUserAuthDto : list){
            KnowledgeBaseDto baseDto = new KnowledgeBaseDto(Integer.parseInt(kmtSysUserAuthDto.getDealType()), kmtSysUserAuthDto.getName());
            result.add(baseDto);
        }
        return result;
    }

    @Override
    public ResponseEntity registQuestonOperater(KmtQuestionOperaterRequest request) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<KmtSysUserAuthDto> list = JSONArray.parseArray( PropertiesUtil.getValue("KMT_SYS_USER_AUTH"), KmtSysUserAuthDto.class);
        KmtSysUserAuthDto target = null;
        for (KmtSysUserAuthDto kmtSysUserAuthDto : list){
            if (Integer.parseInt(kmtSysUserAuthDto.getDealType()) == request.getType()){
                target = kmtSysUserAuthDto;
            }
        }
        if (target != null){
            KmtSysUser sysUser = kmtSysUserService.findKmtSysUserByLoginNameAndType(request.getLoginName(), target.getType());
            if (sysUser != null){
                responseEntity.failure(ResponseConstant.CODE_000, "登录名已存在");
            } else {
                responseEntity.success(ResponseConstant.CODE_999, "成功");
                queryDao.insertLogin(request.getLoginName(), request.getPassword(), request.getName(),  target.getName(), target.getType() + "");
            }
        } else {
            responseEntity.failure(ResponseConstant.CODE_000, "初始化信息出错");
        }
        return responseEntity;
    }
}
