package com.pj.project4sp.admin;

import com.pj.current.config.SystemObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.pj.project4sp.SP;
import com.pj.project4sp.admin4password.SpAdminPasswordService;

import cn.dev33.satoken.stp.StpUtil;

import java.util.List;

/**
 * Service: admin管理员
 *
 * @author kong
 */
@Service
public class SpAdminService {


    @Autowired
    SpAdminMapper spAdminMapper;

    @Autowired
    SpAdminPasswordService spAdminPasswordService;


    /**
     * 管理员添加一个管理员
     *
     * @param admin
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public long add(SpAdmin admin) {
        // 检查姓名是否合法
        SpAdminUtil.checkAdmin(admin);

        // 创建人，为当前账号
        admin.setCreateByAid(StpUtil.getLoginIdAsLong());
        // 开始添加
        spAdminMapper.add(admin);
        // 获取主键
        long id = SP.publicMapper.getPrimarykey();
        // 更改密码（md5与明文）
        spAdminPasswordService.updatePassword(id, admin.getPassword2());

        // 返回主键
        return id;
    }

    /**
     * h5企业注册时添加一个账号
     *
     * @param admin
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public long addWhenRegister(SpAdmin admin) {
        // 检查姓名是否合法
        SpAdminUtil.checkAdmin(admin);

        // 开始添加
        spAdminMapper.add(admin);
        // 获取主键
        long id = SP.publicMapper.getPrimarykey();
        // 更改密码（md5与明文）
        spAdminPasswordService.updatePassword(id, admin.getPassword2());

        // 返回主键
        return id;
    }


    public SpAdmin findByOpenid(String openid) {
        return spAdminMapper.findByOpenid(openid);
    }

    public List<SpAdmin> findByCustomerId(String businessCustomerId) {
        return spAdminMapper.findByCustomerId(businessCustomerId);
    }

    public void editCustomerAdmin(SpAdmin spAdmin) {
        SpAdmin db = spAdminMapper.getById(spAdmin.getId());
        db.setName(spAdmin.getName());
        db.setRoleId(spAdmin.getRoleId());
        db.setStatus(spAdmin.getStatus());
        spAdminMapper.updateAdmin(db);
    }
}
