package com.jiayue.ssi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ssi.backenum.ApproveOperaterEnum;
import com.jiayue.ssi.backenum.ApproveStatusEnum;
import com.jiayue.ssi.constant.ApproveConstants;
import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.entity.SysApprove;
import com.jiayue.ssi.entity.SysUser;
import com.jiayue.ssi.mapper.SysApproveMapper;
import com.jiayue.ssi.mapper.SysUserMapper;
import com.jiayue.ssi.service.SysApproveService;
import com.jiayue.ssi.service.SysUserService;
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 java.util.Date;

/**
* 审核实现类
*
* @author xsl
* @since 2023/03/29
*/
@Service
public class SysApproveServiceImpl extends ServiceImpl<SysApproveMapper, SysApprove> implements SysApproveService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserService sysUserService;

    /**
     * 审核提交
     * @param sysApprove
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public void submitApprove(SysApprove sysApprove) throws CustomException {
        SysApprove newsysApprove = this.getById(sysApprove.getId());
        newsysApprove.setApproveStatus(ApproveStatusEnum.YSP.getCode() + "");
        newsysApprove.setApproveResult(sysApprove.getApproveResult());
        this.updateById(newsysApprove);

        if (newsysApprove.getModuleName().equals(ApproveConstants.MODULE_NAME_USER)){
            // 新增操作
            if (newsysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.INSERT.getCode()))){
                if ("0".equals(newsysApprove.getApproveResult())){
                    // 新增审核通过
                    SysUser sysUser = new SysUser();
                    sysUser.setUsername(newsysApprove.getUsername());
                    sysUser.setIdcard(newsysApprove.getIdcard());
                    sysUser.setUsertype(newsysApprove.getUsertype());
                    sysUser.setNickname(newsysApprove.getNickname());
                    sysUser.setMailbox(newsysApprove.getMailbox());
                    sysUser.setPhonenumber(newsysApprove.getPhonenumber());
                    sysUser.setStatus(newsysApprove.getStatus());
                    sysUser.setExpDate(newsysApprove.getExpDate());
                    sysUser.setSignstr("0");
                    // 离线
                    sysUser.setOnlineStatus("1");
                    sysUserMapper.insert(sysUser);
                }
            }
            else if (newsysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.UPDATE.getCode()))){
                // 修改操作

                if ("0".equals(newsysApprove.getApproveResult())){
                    // 审核通过，修改姓名、邮箱、手机号、有效日期
//                    sysUser.setNickname(newsysApprove.getNickname());
//                    sysUser.setMailbox(newsysApprove.getMailbox());
//                    sysUser.setPhonenumber(newsysApprove.getPhonenumber());
//                    sysUser.setExpDate(newsysApprove.getExpDate());
                    sysUserMapper.updateBaseInfo(Long.parseLong(newsysApprove.getMasterId()), "0", newsysApprove.getNickname(), newsysApprove.getMailbox(), newsysApprove.getPhonenumber(), newsysApprove.getExpDate());
                }
                else{
                    SysUser sysUser = new SysUser();
                    sysUser.setId(Long.parseLong(newsysApprove.getMasterId()));
                    sysUser.setSignstr("0");
                    sysUserMapper.updateById(sysUser);
                }
            }
            else if (newsysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.DELETE.getCode()))){
                // 注销操作
                SysUser sysUser = new SysUser();
                sysUser.setId(Long.parseLong(newsysApprove.getMasterId()));
                if ("0".equals(newsysApprove.getApproveResult())){
                    // 审核通过，修改姓名、邮箱、手机号、有效日期
                    sysUser.setStatus("2");
                }
                sysUser.setSignstr("0");
                sysUserMapper.updateById(sysUser);
            }
            else if (newsysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.APPROVE.getCode()))){
                // 授权操作
                SysUser sysUser = new SysUser();
                sysUser.setId(Long.parseLong(newsysApprove.getMasterId()));
                if ("0".equals(newsysApprove.getApproveResult())){
                    sysUserService.insertUserAuth(Long.parseLong(newsysApprove.getMasterId()), Long.parseLong(newsysApprove.getRoleId()));
                }
                sysUser.setSignstr("0");
                sysUserMapper.updateById(sysUser);
            }
//            else if (newsysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.RELOCK.getCode()))){
//                // 解锁操作
//                SysUser sysUser = new SysUser();
//                sysUser.setId(Long.parseLong(newsysApprove.getMasterId()));
//                if ("0".equals(newsysApprove.getApproveResult())){
//                    // 审核通过，修改姓名、邮箱、手机号、有效日期
//                    sysUser.setStatus("0");
//                    sysUser.setLockTime(0L);
//                    sysUser.setLoginDate(new Date());
//                }
//                sysUser.setSignstr("0");
//                sysUserMapper.updateById(sysUser);
//            }
        }
    }
}
