package com.mosty.base.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.mapper.UserConfigMapper;
import com.mosty.base.service.UserConfigService;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.business.entity.*;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigQuery;
import com.mosty.common.core.business.entity.vo.DeleteDeptVO;
import com.mosty.common.core.business.entity.vo.SysUserAppHomeconfigPlateVo;
import com.mosty.common.core.business.entity.vo.SysUserAppHomeconfigVo;
import com.mosty.common.core.business.mapper.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class UserConfigServiceImpl extends ServiceImpl<UserConfigMapper, SysUserAppHomeconfig>
        implements UserConfigService {

    private final SysUserAppHomeconfigPlateMapper sysUserAppHomeconfigPlateMapper;
    private final SysUserAppHomeconfigPlateListMapper sysUserAppHomeconfigPlateListMapper;
    private final SysAppHomeconfigMapper sysAppHomeconfigMapper;
    private final SysUserMapper sysUserMapper;
    private final SysAppHomeconfigPlateMapper sysAppHomeconfigPlateMapper;
    private final SysAppHomeconfigPlateListMapper sysAppHomeconfigPlateListMapper;

    @Override
    public SysUserAppHomeconfigVo getUserConfig(int userId, int homeId) {
        SysUser user = this.sysUserMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("没有获取到用户信息！！");
        }
        SysUserAppHomeconfigVo vo = new SysUserAppHomeconfigVo();
        // 获取用户配置首页
        SysUserAppHomeconfig homeconfig = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SysUserAppHomeconfig>()
                        .eq(SysUserAppHomeconfig::getHomeid, homeId)
                        .eq(SysUserAppHomeconfig::getUserId, userId)
                        .eq(SysUserAppHomeconfig::getXtZxbz, DeletedEnum.NATURE.code)
        );
        // 获取用户模板配置
        if (homeconfig != null) {
            BeanUtils.copyProperties(homeconfig, vo);
            List<SysUserAppHomeconfigPlate> plateList = this.sysUserAppHomeconfigPlateMapper.selectList(
                    new LambdaQueryWrapper<SysUserAppHomeconfigPlate>()
                            .eq(SysUserAppHomeconfigPlate::getPzId, homeconfig.getId())
                            .eq(SysUserAppHomeconfigPlate::getXtZxbz, DeletedEnum.NATURE.code)
            );
            List<SysUserAppHomeconfigPlateVo> plates = new ArrayList<>();
            plateList.forEach(plate -> {
                SysUserAppHomeconfigPlateVo plateVo = new SysUserAppHomeconfigPlateVo();
                BeanUtils.copyProperties(plate, plateVo);
                // 获取用户配置模块
                List<SysUserAppHomeconfigPlateList> plateItemList = this.sysUserAppHomeconfigPlateListMapper.selectList(
                        new LambdaQueryWrapper<SysUserAppHomeconfigPlateList>()
                                .eq(SysUserAppHomeconfigPlateList::getXtZxbz, DeletedEnum.NATURE.code)
                                .eq(SysUserAppHomeconfigPlateList::getPzPlateId, plate.getId())
                );
                plateVo.setPlateList(plateItemList);
                plates.add(plateVo);
            });
            vo.setPlates(plates);
            return vo;
        } else {
            // 没有获取到用户信息，则保存所有的模板到用户配置表中
            // 保存用户主页
            SysAppHomeconfig config = this.sysAppHomeconfigMapper.selectById(homeId);
            if (config == null) {
                throw new BusinessException("模板在数据库中没有存在，请联系管理员添加");
            }
            homeconfig = new SysUserAppHomeconfig();
            homeconfig.setHomeid(config.getId());
            homeconfig.setHomename(config.getHomename());
            homeconfig.setUserId(user.getId());
            homeconfig.setUserXm(user.getUserName());
            homeconfig.setUserSfzh(user.getIdEntityCard());
            homeconfig.setPzsj(new Date());
            homeconfig.setSd("02");
            BeanUtils.copyProperties(homeconfig, vo);
            // 配置模板
            List<SysAppHomeconfigPlate> plateList = this.sysAppHomeconfigPlateMapper.selectList(
                    new LambdaQueryWrapper<SysAppHomeconfigPlate>()
                            .eq(SysAppHomeconfigPlate::getHomeid, config.getId())
                            .eq(SysAppHomeconfigPlate::getXtZxbz, DeletedEnum.NATURE.code)
            );
            List<SysUserAppHomeconfigPlateVo> plates = new ArrayList<>();
            for (SysAppHomeconfigPlate item : plateList) {
                SysUserAppHomeconfigPlate plate = new SysUserAppHomeconfigPlate();
                SysUserAppHomeconfigPlateVo plateVo = new SysUserAppHomeconfigPlateVo();
                plate.setPzId(homeconfig.getId());
                plate.setPlateId(item.getId());
                plate.setBklx(item.getBklx());
                plate.setBkmc(item.getBkmc());
                plate.setBknrs(item.getBksx());
                plate.setBksx(item.getBksx());
                this.sysUserAppHomeconfigPlateMapper.insert(plate);
                BeanUtils.copyProperties(plate, plateVo);
                // 添加模块
                List<SysAppHomeconfigPlateList> plateItemList = this.sysAppHomeconfigPlateListMapper.selectList(
                        new LambdaQueryWrapper<SysAppHomeconfigPlateList>()
                                .eq(SysAppHomeconfigPlateList::getPlateId, item.getId())
                                .eq(SysAppHomeconfigPlateList::getXtZxbz, DeletedEnum.NATURE.code)
                );
                List<SysUserAppHomeconfigPlateList> plateUserItemList = new ArrayList<>();
                plateItemList.forEach(itemList -> {
                    SysUserAppHomeconfigPlateList pl = new SysUserAppHomeconfigPlateList();
                    pl.setPzId(vo.getId());
                    pl.setPzPlateId(plate.getPlateId());
                    pl.setPlateListId(itemList.getId());
                    pl.setNrmc(itemList.getNrmc());
                    pl.setBksx(itemList.getNrsx());
                    pl.setTzdz(itemList.getTzdz());
                    this.sysUserAppHomeconfigPlateListMapper.insert(pl);
                    plateUserItemList.add(pl);
                });
                plateVo.setPlateList(plateUserItemList);
                plates.add(plateVo);
            }
            vo.setPlates(plates);
            vo.setPzbks(plates.size());
            this.baseMapper.insert(homeconfig);
            return vo;
        }
    }
}
