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

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.SysUserAppHomeconfigPlate;
import com.mosty.common.core.business.entity.SysUserAppHomeconfigPlateList;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigPlateListPage;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigPlateListPage;
import com.mosty.common.core.business.mapper.SysUserAppHomeconfigPlateListMapper;
import com.mosty.common.core.business.service.SysUserAppHomeconfigPlateListService;
import com.mosty.common.token.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 用户app首页配置-板块内容表 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-08-18
 */
@Service
public class SysUserAppHomeconfigPlateListServiceImpl extends ServiceImpl<SysUserAppHomeconfigPlateListMapper, SysUserAppHomeconfigPlateList> implements SysUserAppHomeconfigPlateListService {

    @Resource
    private SysUserAppHomeconfigPlateListMapper sysUserAppHomeconfigPlateListMapper;

    @Override
    public Long saveInfo(SysUserAppHomeconfigPlateList plate, String ipAddress) {
        plate.setXtCjsj(new Date());
        plate.setXtLrip(ipAddress);
        plate.setXtLrsj(new Date());
        plate.setXtZhxgsj(new Date());
        plate.setXtZxbz(DeletedEnum.NATURE.code);
        sysUserAppHomeconfigPlateListMapper.insert(plate);
        return plate.getId();
    }

    @Override
    public boolean saveInfoBatch(List<SysUserAppHomeconfigPlateList> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            Long pzId = plates.get(0).getPzId();
            Long pzPlateId = plates.get(0).getPzPlateId();
            QueryWrapper<SysUserAppHomeconfigPlateList> queryWrapper = new QueryWrapper<>();
            queryWrapper.and(wrapper -> wrapper.eq("pz_id", pzId));
            queryWrapper.and(wrapper -> wrapper.eq("pz_plate_id", pzPlateId));
            queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
            List<SysUserAppHomeconfigPlateList> list = sysUserAppHomeconfigPlateListMapper.selectList(queryWrapper);
            for(SysUserAppHomeconfigPlateList plate : plates) {
                boolean bool = isExist(list, plate.getPzId(), plate.getPzPlateId(), plate.getPlateListId());
                if(!bool) {
                    QueryWrapper<SysUserAppHomeconfigPlateList> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.and(wrapper -> wrapper.eq("pz_id", plate.getPzId()));
                    queryWrapper1.and(wrapper -> wrapper.eq("pz_plate_id", plate.getPzPlateId()));
                    queryWrapper1.and(wrapper -> wrapper.eq("plate_list_id", plate.getPlateListId()));
                    SysUserAppHomeconfigPlateList plateList =
                            sysUserAppHomeconfigPlateListMapper.selectOne(queryWrapper1);
                    if(plateList != null) {
                        sysUserAppHomeconfigPlateListMapper.updateById(plateList.getPzId(), plateList.getPzPlateId(),
                                plateList.getPlateListId(), DeletedEnum.NATURE.code);
                    }
                }
                if(!CollectionUtils.isEmpty(list)) {
                    // list 库中   plateList 参数,
                    for(SysUserAppHomeconfigPlateList plateList : list) {
                        boolean bools = isExist(plates, plateList.getPzId(), plateList.getPzPlateId(),
                                plateList.getPlateListId());
                        if(!bools) {
                            sysUserAppHomeconfigPlateListMapper.updateById(plate.getPzId(), plate.getPzPlateId(),
                                    plate.getPlateListId(), DeletedEnum.DELETED.code);
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * 判断用户配置板块是否存在
     * @param list
     * @param pzid
     * @param pzPlateId
     * @return
     */
    public boolean isExist(List<SysUserAppHomeconfigPlateList> list, Long pzid, Long pzPlateId, Long plateListId) {
        if(CollectionUtils.isEmpty(list)) {
            return false;
        }
        for(SysUserAppHomeconfigPlateList plate : list) {
            if(plate.getPzId().longValue() == pzid.longValue() && plate.getPzPlateId().longValue() == pzPlateId.longValue() && plate.getPlateListId().longValue() == plateListId.longValue()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void updateInfo(SysUserAppHomeconfigPlateList plate, String ipAddress) {
        SysUserAppHomeconfigPlateList oldConfig = sysUserAppHomeconfigPlateListMapper.selectById(plate.getId());
        Asserts.check(oldConfig == null, "主键为 %s 的信息不存在", oldConfig.getId());
        plate.setXtZxbz(DeletedEnum.NATURE.code);
        plate.setXtZhxgsj(new Date());
        plate.setXtLrip(ipAddress);
        sysUserAppHomeconfigPlateListMapper.updateById(plate);
    }

    @Override
    public void updateInfoBatch(List<SysUserAppHomeconfigPlateList> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            sysUserAppHomeconfigPlateListMapper.deleteByPzId(plates.get(0).getPzId(), plates.get(0).getPzPlateId());
            for(SysUserAppHomeconfigPlateList plate : plates) {
                plate.setXtCjsj(new Date());
                plate.setXtLrip(ipAddress);
                plate.setXtLrsj(new Date());
                plate.setXtZhxgsj(new Date());
                plate.setXtZxbz(DeletedEnum.NATURE.code);
                sysUserAppHomeconfigPlateListMapper.insert(plate);
            }
        }
    }

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

    @Override
    public IPage<SysUserAppHomeconfigPlateList> selectByPage(SysUserAppHomeconfigPlateListPage cofigPage) {
        QueryWrapper<SysUserAppHomeconfigPlateList> queryWrapper = new QueryWrapper<>();
        if (cofigPage.getPzId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("pz_id", cofigPage.getPzId()));
        }
        if (cofigPage.getPzPlateId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("pz_plate_id", cofigPage.getPzPlateId()));
        }
        if (cofigPage.getPlateListId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("plate_list_id", cofigPage.getPlateListId()));
        }
        if (StringUtils.isNotBlank(cofigPage.getNrmc())) {
            queryWrapper.and(wrapper -> wrapper.like("nrmc", cofigPage.getNrmc()));
        }
        queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
        List<OrderItem> orderBy = Lists.newArrayList(OrderItem.asc("id"));
        cofigPage.setOrders(orderBy);
        return sysUserAppHomeconfigPlateListMapper.selectPage(cofigPage, queryWrapper);
    }
}
