package com.ruoyi.orderManagement.BasicData.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.orderManagement.BasicData.domain.FleetDriverMsg;
import com.ruoyi.orderManagement.BasicData.mapper.FleetDriverMsgMapper;
import com.ruoyi.orderManagement.BasicData.service.IFleetDriverMsgService;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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


/**
 * 【司机】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@Service
public class FleetDriverMsgServiceImpl implements IFleetDriverMsgService
{
    @Autowired
    private FleetDriverMsgMapper fleetDriverMsgMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询【司机】
     * 
     * @param id 【司机】ID
     * @return 【司机】
     */
    @Override
    public FleetDriverMsg selectFleetDriverMsgById(Long id)
    {
        return fleetDriverMsgMapper.selectFleetDriverMsgById(id);
    }

    /**
     * 查询【司机】列表
     * 
     * @param fleetDriverMsg 【司机】
     * @return 【司机】
     */
    @Override
    public List<FleetDriverMsg> selectFleetDriverMsgList(FleetDriverMsg fleetDriverMsg)
    {
        return fleetDriverMsgMapper.selectFleetDriverMsgList(fleetDriverMsg);
    }

    @Override
    public List<Map<String, Object>> selectFleetDriverMsgListMap(FleetDriverMsg fleetDriverMsg) {
        return fleetDriverMsgMapper.selectFleetDriverMsgListMap(fleetDriverMsg);
    }

    /**
     * 新增【司机】
     * 
     * @param fleetDriverMsg 【司机】
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertFleetDriverMsg(FleetDriverMsg fleetDriverMsg)
    {
        // 检验必填
        if(StringUtils.isNull(fleetDriverMsg.getFleetCompanyId()) ||
                StringUtils.isNull(fleetDriverMsg.getName()) ||
                StringUtils.isNull(fleetDriverMsg.getIdcarNum()) ||
                StringUtils.isNull(fleetDriverMsg.getTel()) ){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("请填写必输项");
        }
        if(null!=fleetDriverMsg.getId() && !fleetDriverMsg.getId().equals("")){
            fleetDriverMsg.setUpdateBy(SecurityUtils.getUsername());
            fleetDriverMsg.setUpdateTime(new Date());
            fleetDriverMsgMapper.updateFleetDriverMsg(fleetDriverMsg);
            return AjaxResult.success();
        }else {
            // 添加用户表
            SysUser sysUser = new SysUser();
            sysUser.setUserName(fleetDriverMsg.getTel());
            sysUser.setNickName(fleetDriverMsg.getName());
            sysUser.setPhonenumber(fleetDriverMsg.getTel());
            //sysUser.setEmail(fleetDriverMsg.get);
            sysUser.setPassword(SecurityUtils.encryptPassword(fleetDriverMsg.getTel()));
            sysUser.setDeptId(120L); // 当前给定、后期修改
            Long [] rolr = {110L}; // 当前给定、后期修改
            sysUser.setRoleIds(rolr);

            // 新增用户信息
            int rows = userMapper.insertUser(sysUser);
            if(rows <= 0 ){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("请填写必输项");
            }
            // 新增用户岗位关联
            insertUserPost(sysUser);
            // 新增用户与角色管理
            insertUserRole(sysUser);

            fleetDriverMsg.setCreateBy(SecurityUtils.getUsername());
            fleetDriverMsg.setCreatTime(new Date());
            fleetDriverMsgMapper.insertFleetDriverMsg(fleetDriverMsg);
            return AjaxResult.success();
        }
    }

    /**
     * 修改【司机】
     * 
     * @param fleetDriverMsg 【司机】
     * @return 结果
     */
    @Override
    public int updateFleetDriverMsg(FleetDriverMsg fleetDriverMsg)
    {
        fleetDriverMsg.setUpdateTime(DateUtils.getNowDate());
        return fleetDriverMsgMapper.updateFleetDriverMsg(fleetDriverMsg);
    }

    /**
     * 批量删除【司机】
     * 
     * @param ids 需要删除的【司机】ID
     * @return 结果
     */
    @Override
    public int deleteFleetDriverMsgByIds(Long[] ids)
    {
        return fleetDriverMsgMapper.deleteFleetDriverMsgByIds(ids);
    }

    /**
     * 删除【司机】信息
     * 
     * @param id 【司机】ID
     * @return 结果
     */
    @Override
    public int deleteFleetDriverMsgById(Long id)
    {
        return fleetDriverMsgMapper.deleteFleetDriverMsgById(id);
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertUserPost(SysUser user) {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotNull(posts)) {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (Long postId : posts) {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0) {
                userPostMapper.batchUserPost(list);
            }
        }
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user) {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                userRoleMapper.batchUserRole(list);
            }
        }
    }



}
