package com.xhkjedu.suser.service.system;

import com.xhkjedu.suser.mapper.system.UsecretMapper;
import com.xhkjedu.suser.mapper.system.UserUsecretMapper;
import com.xhkjedu.suser.model.system.TUsecret;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UsecretService {
    @Resource
    private UsecretMapper usecretMapper;
    @Resource
    private UserUsecretMapper userUsecretMapper;

    /**
      * 添加
      * @param [usecret]
      * @author ywx
      * @date 2022/3/30 11:41
      * @return void
      */
    public void save(TUsecret usecret) {
        usecret.setCreatetime(N_Utils.getSecondTimestamp());
        usecretMapper.insertSelective(usecret);
    }

    /**
      * 修改
      * @param [usecret]
      * @author ywx
      * @date 2022/3/30 11:41
      * @return com.xhkjedu.vo.ResultVo
      */
    public ResultVo update(TUsecret usecret) {
        Integer usid = usecret.getUsid();
        Integer usenum = userUsecretMapper.getUseNumByUsId(usid);//获取密保问题使用频次
        if (usenum != 0) return new ResultVo(1,"该密保问题被使用禁止修改");
        usecret.setCreatetime(N_Utils.getSecondTimestamp());
        usecretMapper.updateByPrimaryKeySelective(usecret);
        return new ResultVo(0,"修改成功");
    }

    /**
     * 删除
     *
     * @param [usid]
     * @param schoolid
     * @return com.xhkjedu.vo.ResultVo
     * @author ywx
     * @date 2022/3/30 11:41
     */
    public ResultVo deleteById(Integer usid, Integer schoolid) {
        Integer usenum = userUsecretMapper.getUseNumByUsId(usid);//获取密保问题使用频次
        if (usenum != 0) return new ResultVo(1, "该密保问题被使用禁止删除");
        Integer num = usecretMapper.getNumBySchoolId(schoolid);//获取密保问题数量
        if (num.compareTo(2) != 1) return new ResultVo(1, "密保问题少于两个禁止删除");
        usecretMapper.deleteByPrimaryKey(usid);
        return new ResultVo(0, "删除成功");
    }

    /**
      * 列表
      * @param [usecret]
      * @author ywx
      * @date 2022/3/30 11:41
      * @return java.util.List<java.util.Map>
      */
    public List<Map> findAll(TUsecret usecret) {
        return usecretMapper.findAll(usecret);
    }

    /**
      * 选择列表
      * @param [schoolid]
      * @author ywx
      * @date 2022/3/30 13:17
      * @return java.util.List<java.util.Map>
      */
    public List<Map> listForSel(Integer schoolid) {
        return usecretMapper.listForSel(schoolid);
    }
}
