package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.SysDepart;
import cn.com.ctop.common.module.entity.SysUser;
import cn.com.ctop.common.module.entity.SysUserDepart;
import cn.com.ctop.common.module.entity.UserCompany;
import cn.com.ctop.common.module.mapper.SysDepartMapper;
import cn.com.ctop.common.module.mapper.SysUserDepartMapper;
import cn.com.ctop.common.module.mapper.UserCompanyMapper;
import cn.com.ctop.common.module.service.IUserCompanyService;
import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;

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

/**
 * 用户够公司
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-11-14
 */
@Slf4j
@Service
public class UserCompanyServiceImpl extends ServiceImpl<UserCompanyMapper, UserCompany> implements IUserCompanyService {
    @Resource
    private SysUserDepartMapper userDepartMapper;
    @Resource
    private SysDepartMapper departMapper;
    @Resource
    private UserCompanyMapper userCompanyMapper;

    @Override
    public void addOrUpdateUserWithCompany(SysUser user) {
        SysUserDepart userDepart = userDepartMapper.getUserDepartByUserId(user.getId());
        if (!Check.isNull(userDepart)) {
            String depId = userDepart.getDepId();
            for (int i = 0; i <= 10; i++) {
                SysDepart depart = departMapper.queryUserDepartsByDepartId(depId);
                if (Check.isNull(depart)) {
                    break;
                }
                if (!"1".equals(depart.getOrgType())) {
                    depId = depart.getParentId();
                    continue;
                } else {
                    UserCompany userCompany = new UserCompany();
                    userCompany.setUserId(user.getId());
                    userCompany.setUserName(user.getRealname());
                    userCompany.setCompanyId(depart.getId());
                    userCompany.setCompanyName(depart.getDepartName());
                    QueryWrapper<UserCompany> userCompanyQueryWrapper = new QueryWrapper();
                    userCompanyQueryWrapper.eq("user_id", user.getId());
                    userCompanyQueryWrapper.last("limit 1");
                    UserCompany addUserCompany = userCompanyMapper.selectOne(userCompanyQueryWrapper);
                    int j;
                    if (Check.isNull(addUserCompany)) {
                        userCompany.setCreateTime(new Date());
                        userCompany.setUpdateTime(new Date());
                        j = userCompanyMapper.insert(userCompany);
                    } else {
                        QueryWrapper<UserCompany> updateUserCompanyQueryWrapper = new QueryWrapper();
                        updateUserCompanyQueryWrapper.eq("user_id", user.getId());
                        userCompany.setUpdateTime(new Date());
                        j = userCompanyMapper.update(userCompany, updateUserCompanyQueryWrapper);
                    }

                    if (j > 0) {
                        log.info("同步用户公司信息完成,userId:{}", user.getId());
                    }
                    break;
                }
            }

        }
    }

    /**
     * 删除公司信息
     *
     * @param
     */
    @Override
    public void removeByUserId(String userId) {
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("user_id", userId);
        userCompanyMapper.deleteByMap(deleteMap);

    }

    @Override
    public UserCompany getByUserId(String userId) {
        QueryWrapper<UserCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.last("limit 1");
        return null;
    }

    @Override
    public Result<Object> allCompany() {
        List<JSONObject> data = userCompanyMapper.allCompany();
        return Result.ok(data);
    }

    @Override
    public JSONObject getCompanyInfoByAccountId(Long accountId) {
        return userCompanyMapper.getCompanyInfoByAccountId(accountId);
    }

    @Override
    public JSONObject getCompanyInfoByUserId(String clipId) {
        return userCompanyMapper.getCompanyInfoByUserId(clipId);
    }
}
