package com.mosty.common.core.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.core.business.entity.SysAppHomeconfig;
import com.mosty.common.core.business.entity.SysAppHomeconfigPlate;
import com.mosty.common.core.business.entity.SysAppHomeconfigPlateList;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigPage;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigQuery;
import com.mosty.common.core.business.entity.vo.SysAppHomeconfigPlateVo;
import com.mosty.common.core.business.entity.vo.SysAppHomeconfigRsVO;
import com.mosty.common.core.business.entity.vo.SysAppHomeconfigVO;
import com.mosty.common.core.business.mapper.SysAppHomeconfigMapper;
import com.mosty.common.core.business.mapper.SysAppHomeconfigPlateListMapper;
import com.mosty.common.core.business.mapper.SysAppHomeconfigPlateMapper;
import com.mosty.common.core.business.service.SysAppHomeconfigService;
import com.mosty.common.token.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * app首页表 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-08-18
 */
@Service
public class SysAppHomeconfigServiceImpl extends ServiceImpl<SysAppHomeconfigMapper, SysAppHomeconfig> implements SysAppHomeconfigService {

    @Resource
    private SysAppHomeconfigMapper sysAppHomeconfigMapper;

    @Resource
    private SysAppHomeconfigPlateMapper sysAppHomeconfigPlateMapper;

    @Resource
    private SysAppHomeconfigPlateListMapper sysAppHomeconfigPlateListMapper;

    @Override
    public Long saveInfo(SysAppHomeconfigVO configVO, String ipAddress) {
        SysAppHomeconfig config = new SysAppHomeconfig();
        BeanUtils.copyProperties(configVO, config);
        config.setXtZhxgsj(new Date());
        config.setXtCjsj(new Date());
        config.setXtLrip(ipAddress);
        config.setBz(configVO.getBz());
        config.setXtLrsj(new Date());
        config.setXtZhxgsj(new Date());
        config.setXtZxbz(DeletedEnum.NATURE.code);
        sysAppHomeconfigMapper.insert(config);
        return config.getId();
    }

    @Override
    public void updateInfo(SysAppHomeconfigVO configVO, String ipAddress) {
        SysAppHomeconfig oldConfig = this.getBaseMapper().selectById(configVO.getId());
        Asserts.check(oldConfig == null, "主键为 %s 的应用信息不存在", configVO.getId());
        SysAppHomeconfig config = BeanUtil.copyProperties(configVO, SysAppHomeconfig.class);
        config.setXtZxbz(DeletedEnum.NATURE.code);
        config.setXtZhxgsj(new Date());
        config.setXtLrip(ipAddress);
        sysAppHomeconfigMapper.updateById(config);
    }

    @Override
    public void delete(List<Long> list, String ipAddress, UserInfo userInfo) {
        if(!CollectionUtils.isEmpty(list)) {
            for(Long id : list) {
                SysAppHomeconfig config = getBaseMapper().selectById(id);
                if(config != null) {
                    config.setXtZhxgsj(new Date());
                    config.setXtZxbz(DeletedEnum.DELETED.code);
                    config.setXtZhxgsj(new Date());
                    config.setXtZhxgip(ipAddress);
                    if(userInfo != null) {
                        config.setXtZhxgrxm(userInfo.getUserName());
                        config.setXtZhxgrbmid(userInfo.getDeptId() + "");
                        config.setXtZhxgrbm(userInfo.getDeptName());
                    }
                    sysAppHomeconfigMapper.updateById(config);
                }
            }
        }
    }

    @Override
    public IPage<SysAppHomeconfig> selectByPage(SysAppHomeconfigPage cofigPage) {
        QueryWrapper<SysAppHomeconfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(cofigPage.getHomename())) {
            queryWrapper.and(wrapper -> wrapper.like("homename", cofigPage.getHomename()));
        }
        queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
        List<OrderItem> orderBy = Lists.newArrayList(OrderItem.asc("id"));
        cofigPage.setOrders(orderBy);
        return sysAppHomeconfigMapper.selectPage(cofigPage, queryWrapper);
    }

    @Override
    public SysAppHomeconfigRsVO queryConfig(SysAppHomeconfigQuery configQuery) {
        List<SysAppHomeconfigPlateList> plateList = new ArrayList<>();
        SysAppHomeconfigRsVO vo = new SysAppHomeconfigRsVO();
        if (configQuery.getId() == null) {
            return vo;
        }
        SysAppHomeconfig config = sysAppHomeconfigMapper.selectById(configQuery.getId());
        BeanUtils.copyProperties(config, vo);
        QueryWrapper<SysAppHomeconfigPlate> plateWrapper = new QueryWrapper<>();
        plateWrapper.lambda().eq(SysAppHomeconfigPlate::getHomeid, config.getId());
        plateWrapper.lambda().eq(SysAppHomeconfigPlate::getXtZxbz, DeletedEnum.NATURE.code);
        plateWrapper.orderByAsc("bksx");
        List<SysAppHomeconfigPlate> plates = sysAppHomeconfigPlateMapper.selectList(plateWrapper);
        if(!CollectionUtils.isEmpty(plates)) {
            List<Long> plateIds = plates.stream().map(item->item.getId()).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(plateIds)) {
                QueryWrapper<SysAppHomeconfigPlateList> plateListWrapper = new QueryWrapper<>();
                plateListWrapper.lambda().in(SysAppHomeconfigPlateList::getPlateId, plateIds);
                plateListWrapper.lambda().eq(SysAppHomeconfigPlateList::getXtZxbz, DeletedEnum.NATURE.code);
                plateListWrapper.orderByAsc("nrsx");
                plateList = sysAppHomeconfigPlateListMapper.selectList(plateListWrapper);
            }
            List<SysAppHomeconfigPlateVo> plateVos = new ArrayList<>();
            for(SysAppHomeconfigPlate plate : plates) {
                SysAppHomeconfigPlateVo plateVo = new SysAppHomeconfigPlateVo();
                BeanUtils.copyProperties(plate, plateVo);
                if(!CollectionUtils.isEmpty(plateList)) {
                    List<SysAppHomeconfigPlateList> list =
                            plateList.parallelStream().filter(item->item.getPlateId().longValue()== plate.getId().longValue()).collect(Collectors.toList());
                    plateVo.setPlateList(list);
                }
                plateVos.add(plateVo);
            }
            vo.setPlateList(plateVos);
        }
        return vo;
    }
}
