package com.xhpc.card.service.impl;

import com.xhpc.card.domain.CardClientUser;
import com.xhpc.card.mapper.IXhpcCardClientUserMapper;
import com.xhpc.card.service.IXhpcCardClientUserService;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author yuyang
 * @date 2022/3/21 9:46
 */
@Service
public class XhpcCardClientUserServiceImpl extends BaseService implements IXhpcCardClientUserService {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private IXhpcCardClientUserMapper xhpcCardClientUserMapper;

    @Override
    public List<Map<String, Object>> selectXhpcCardClientUserList(String userName, Integer usersLevel, Long operatorId, String tenantId) {

        LoginUser loginUser = tokenService.getLoginUser();
        Long logUserId = loginUser.getUserid();
        SysUser sysUser = loginUser.getSysUser();
        List<Map<String, Object>> list = new ArrayList<>();
        startPage();
        if (logUserId != UserTypeUtil.USER_ID) {
            Long logOperatorId = sysUser.getOperatorId();
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                //运营商看自己的场站
                list = xhpcCardClientUserMapper.selectXhpcCardClientUserList(userName,usersLevel,operatorId,tenantId,1,logOperatorId);
            } else {
                //查询赋值的场站
                list = xhpcCardClientUserMapper.selectXhpcCardClientUserList(userName,usersLevel,operatorId,tenantId,2,logOperatorId);
            }
        }else{
            list = xhpcCardClientUserMapper.selectXhpcCardClientUserList(userName,usersLevel,operatorId,tenantId,0,null);
        }
        return list;
    }

    /**
     * 添加
     *
     * @param cardClientUser
     * @return
     */
    @Override
    public AjaxResult addOrUpdateXhpcCardClientUser(CardClientUser cardClientUser) {

        if(cardClientUser.getUsersName() ==null || "".equals(cardClientUser.getUsersName())){
            return AjaxResult.error("用户名称为空");
        }
        if(cardClientUser.getUsersID()==null){
            Map<String, Object> xhpcCardClientUserById = xhpcCardClientUserMapper.getXhpcCardClientUserById(1,null, cardClientUser.getUsersName());
            if(xhpcCardClientUserById !=null &&"0".equals(xhpcCardClientUserById.get("delFlag").toString())){
                return AjaxResult.error("用户名称已存在");
            }
        }else{
            Map<String, Object> xhpcCardClientUserById = xhpcCardClientUserMapper.getXhpcCardClientUserById(1,cardClientUser.getUsersID(), cardClientUser.getUsersName());
            if(xhpcCardClientUserById !=null && "0".equals(xhpcCardClientUserById.get("delFlag").toString())){
                return AjaxResult.error("用户名称已存在");
            }
        }
        if(cardClientUser.getUsersPwd() ==null || "".equals(cardClientUser.getUsersPwd())){
            return AjaxResult.error("用户密码为空");
        }
        if(cardClientUser.getTenantId() ==null || "".equals(cardClientUser.getTenantId())){
            return AjaxResult.error("租户为空");
        }
        if(cardClientUser.getUsersLevel() ==null){
            return AjaxResult.error("级别为空");
        }

        if(cardClientUser.getCorpNo() ==null || "".equals(cardClientUser.getCorpNo())){
            return AjaxResult.error("运营商为空");
        }

        if(cardClientUser.getUsersID()==null){
            int i = xhpcCardClientUserMapper.insertXhpcCardClientUser(cardClientUser);
            if(i==0){
                return AjaxResult.error("添加失败");
            }
        }else{
            int i = xhpcCardClientUserMapper.updateXhpcCardClientUser(cardClientUser);
            if(i==0){
                return AjaxResult.error("添加失败");
            }
        }

        return AjaxResult.success();
    }

    /**
     * 获取运营商接口
     *
     * @param tenantId 租户id
     * @return
     */
    @Override
    public AjaxResult getoperatorList(String tenantId) {
        return AjaxResult.success(xhpcCardClientUserMapper.getoperatorList(tenantId));
    }

    /**
     * 详情
     *
     * @param usersID 用户Id
     * @return
     */
    @Override
    public Map<String, Object> getXhpcCardClientUserById(Integer usersID) {
        return xhpcCardClientUserMapper.getXhpcCardClientUserById(0,usersID,null);
    }

    /**
     * 获取运营商接口
     *
     * @param usersID
     * @param status
     * @return
     */
    @Override
    public AjaxResult getXhpcCardClientUserStatus(Integer usersID, Integer status) {
        CardClientUser cardClientUser =new CardClientUser();
        cardClientUser.setUsersID(usersID);
        cardClientUser.setStatus(status);
        int i = xhpcCardClientUserMapper.updateXhpcCardClientUser(cardClientUser);
        if(i==0){
            return AjaxResult.error("状态改变失败");
        }else{
            return AjaxResult.success("状态改变成功");
        }
    }
}
