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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.entity.*;
import com.zhgd.xmgl.modules.basicdata.mapper.EnterpriseMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.ProjectMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.IEnterpriseMainLicenseService;
import com.zhgd.xmgl.modules.basicdata.service.IEnterpriseMainService;
import com.zhgd.xmgl.modules.basicdata.service.IEnterpriseService;
import com.zhgd.xmgl.modules.basicdata.vo.EnterpriseVo;
import com.zhgd.xmgl.security.SecurityUtil;
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;
import java.util.stream.Collectors;

/**
 * @Description: 企业信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private IEnterpriseMainService enterpriseMainService;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IEnterpriseMainLicenseService enterpriseMainLicenseService;

    @Override
    public Page<Enterprise> pageList(Map<String, Object> map) {
        Page<Enterprise> page = PageUtil.getPage(map);
        QueryWrapper<Enterprise> wrapper = QueryGenerator.initPageQueryWrapper(Enterprise.class, map);
        return this.page(page, wrapper);
    }

    @Override
    public boolean saveInfo(EnterpriseVo enterpriseVo) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        enterpriseVo.setEnterpriseSn(uuid);
        enterpriseVo.setCreateTime(new Date());
        boolean flag = this.save(enterpriseVo);
        if (flag) {
            SystemUser systemUser = new SystemUser();
            systemUser.setAccount(enterpriseVo.getAccount());
            systemUser.setPassword(enterpriseVo.getPassword());
            systemUser.setShowPassword(enterpriseVo.getPassword());
            systemUser.setRealName(enterpriseVo.getRealName());
            systemUser.setIsManager(true);
            systemUser.setState(0);
            systemUser.setUserTel(enterpriseVo.getPhone());
            systemUser.setSn(enterpriseVo.getEnterpriseSn());
            systemUser.setAccountType(3);
            systemUser.setEmail(enterpriseVo.getEmail());
            systemUser.setCreateTime(new Date());
            systemUserMapper.insert(systemUser);
            for (EnterpriseMain enterpriseMain : enterpriseVo.getEnterpriseMains()) {
                enterpriseMain.setEnterpriseSn(enterpriseVo.getEnterpriseSn());
                enterpriseMainService.save(enterpriseMain);
                for (EnterpriseMainLicense licenses : enterpriseMain.getLicenses()) {
                    licenses.setEnterpriseSn(enterpriseVo.getEnterpriseSn());
                    licenses.setMainId(enterpriseMain.getId());
                }
                enterpriseMainLicenseService.saveBatch(enterpriseMain.getLicenses());
            }
        }
        return flag;

    }

    @Override
    public boolean updateInfo(Enterprise enterprise, Integer suggest) {
        SystemUser systemUser = systemUserMapper.selectOne(Wrappers.<SystemUser>lambdaQuery()
                .eq(SystemUser::getSn, enterprise.getEnterpriseSn()).eq(SystemUser::getIsManager, true));
        if (suggest == 1) {
            LambdaUpdateWrapper<Enterprise> enterpriseWrapper = Wrappers.<Enterprise>lambdaUpdate();
            enterpriseWrapper.set(Enterprise::getState, 1);
            enterpriseWrapper.eq(Enterprise::getEnterpriseId, enterprise.getEnterpriseId());
            this.update(enterpriseWrapper);
            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());
            enterpriseMainLicenseService.remove(Wrappers.<EnterpriseMainLicense>lambdaQuery()
                    .eq(EnterpriseMainLicense::getEnterpriseSn, enterprise.getEnterpriseSn()));
            enterpriseMainService.remove(Wrappers.<EnterpriseMain>lambdaQuery().eq(EnterpriseMain::getEnterpriseSn, enterprise.getEnterpriseSn()));
            this.removeById(enterprise.getEnterpriseId());
        }
        return true;
    }

    @Override
    public EnterpriseVo getDetailById(Long enterpriseId) {
        EnterpriseVo enterpriseVo = baseMapper.getDetailById(enterpriseId);
        List<EnterpriseMain> enterpriseMains = enterpriseMainService.list(Wrappers.<EnterpriseMain>lambdaQuery()
                .eq(EnterpriseMain::getEnterpriseSn, enterpriseVo.getEnterpriseSn()));
        List<EnterpriseMainLicense> enterpriseMainLicens = enterpriseMainLicenseService.list(Wrappers.<EnterpriseMainLicense>lambdaQuery()
                .eq(EnterpriseMainLicense::getEnterpriseSn, enterpriseVo.getEnterpriseSn()));
        for (EnterpriseMain enterpriseMain : enterpriseMains) {
            enterpriseMain.setLicenses(enterpriseMainLicens.stream().filter(l -> l.getMainId().equals(enterpriseMain.getId()))
                    .collect(Collectors.toList()));
        }
        enterpriseVo.setEnterpriseMains(enterpriseMains);
        return enterpriseVo;
    }

    @Override
    public List<Enterprise> listByMainType(Integer mainType) {
        Project project = projectMapper.selectOne(Wrappers.<Project>lambdaQuery().eq(Project::getProjectSn, SecurityUtil.getUser().getSn()));
        return baseMapper.listByMainType(mainType, project.getGovernmentSn());
    }
}
