package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.ProjectDeviceDto;
import com.zhgd.xmgl.modules.basicdata.dto.ProjectDto;
import com.zhgd.xmgl.modules.basicdata.entity.Project;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.mapper.ProjectMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.modules.basicdata.vo.ProjectVo;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 项目信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {


    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public Page<ProjectDto> getPageList(Map<String, Object> map) {
        SecurityUser user = SecurityUtil.getUser();
        Page<Project> page = PageUtil.getPage(map);
        QueryWrapper<Project> wrapper = QueryGenerator.initPageQueryWrapper(Project.class, map);
        wrapper.eq("p.government_sn", user.getSn());
        Integer state = MapUtils.getInteger(map, "state");
        if (state != null) {
            wrapper.eq("p.state", state);
        }
        return baseMapper.getPageList(page, wrapper);
    }

    @Override
    public boolean saveInfo(ProjectVo projectVo) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        projectVo.setProjectSn(uuid);
        projectVo.setCreateTime(new Date());
        boolean flag = this.save(projectVo);
        if (flag) {
            SystemUser systemUser = new SystemUser();
            systemUser.setAccount(projectVo.getAccount());
            systemUser.setPassword(projectVo.getPassword());
            systemUser.setShowPassword(projectVo.getPassword());
            systemUser.setIsManager(true);
            systemUser.setState(0);
            systemUser.setUserTel(projectVo.getPhone());
            systemUser.setSn(projectVo.getProjectSn());
            systemUser.setAccountType(4);
            systemUser.setEmail(projectVo.getEmail());
            systemUser.setCreateTime(new Date());
            systemUserMapper.insert(systemUser);
        }
        return flag;
    }

    @Override
    public boolean examine(Project project, Integer suggest) {
        SystemUser systemUser = systemUserMapper.selectOne(Wrappers.<SystemUser>lambdaQuery()
                .eq(SystemUser::getSn, project.getProjectSn()).eq(SystemUser::getIsManager, true));
        if (suggest == 1) {
            LambdaUpdateWrapper<Project> projectWrapper = Wrappers.<Project>lambdaUpdate();
            projectWrapper.set(Project::getState, 1);
            projectWrapper.set(Project::getApproveTime, new Date());
            projectWrapper.eq(Project::getProjectId, project.getProjectId());
            this.update(projectWrapper);
            LambdaUpdateWrapper<SystemUser> userWrapper = Wrappers.<SystemUser>lambdaUpdate();
            userWrapper.set(SystemUser::getState, 1);
            userWrapper.eq(SystemUser::getUserId, systemUser.getUserId());
            systemUserMapper.update(systemUser, userWrapper);
        } else {
            systemUserMapper.deleteById(systemUser.getUserId());
            this.removeById(project.getProjectId());
        }
        return true;
    }

    @Override
    public List<String> getSnListForGov(String governmentSn) {
        return baseMapper.getSnListForGov(governmentSn);
    }

    @Override
    public Page<ProjectDeviceDto> pageListForGov(Map<String, Object> map) {
        LambdaQueryWrapper<Project> wrapper = Wrappers.<Project>lambdaQuery();
        String projectName = MapUtils.getString(map, "projectName");
        SecurityUser user = SecurityUtil.getUser();
        wrapper.eq(Project::getGovernmentSn, user.getSn());
        if (StringUtils.isNotBlank(projectName)) {
            wrapper.like(Project::getProjectName, projectName);
        }
        Page<Project> page = PageUtil.getPage(map);
        return baseMapper.pageListByGov(page, wrapper);
    }
}
