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.SysUserAppHomeconfig;
import com.mosty.common.core.business.entity.SysUserAppHomeconfigPlate;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigPlatePage;
import com.mosty.common.core.business.mapper.SysUserAppHomeconfigPlateMapper;
import com.mosty.common.core.business.service.SysUserAppHomeconfigPlateService;
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 SysUserAppHomeconfigPlateServiceImpl extends ServiceImpl<SysUserAppHomeconfigPlateMapper, SysUserAppHomeconfigPlate> implements SysUserAppHomeconfigPlateService {

    @Resource
    private SysUserAppHomeconfigPlateMapper sysUserAppHomeconfigPlateMapper;

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

    @Override
    public boolean saveInfoBatch(List<SysUserAppHomeconfigPlate> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            Long pzId = plates.get(0).getPzId();
//            sysUserAppHomeconfigPlateMapper.deleteByPzid(pzId);
            QueryWrapper<SysUserAppHomeconfigPlate> queryWrapper = new QueryWrapper<>();
            queryWrapper.and(wrapper -> wrapper.eq("pz_id", pzId));
            queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
            List<SysUserAppHomeconfigPlate> list = sysUserAppHomeconfigPlateMapper.selectList(queryWrapper);
            for(SysUserAppHomeconfigPlate plate : plates) {
                boolean bool = isExist(list, plate.getPzId(), plate.getPlateId());

                if(!bool) {
                    QueryWrapper<SysUserAppHomeconfigPlate> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.and(wrapper -> wrapper.eq("pz_id", plate.getPzId()));
                    queryWrapper1.and(wrapper -> wrapper.eq("plate_id", plate.getPlateId()));
                    SysUserAppHomeconfigPlate userPlate =
                            sysUserAppHomeconfigPlateMapper.selectOne(queryWrapper1);
//                    plate.setXtCjsj(new Date());
//                    plate.setXtLrip(ipAddress);
//                    plate.setXtLrsj(new Date());
//                    plate.setXtZhxgsj(new Date());
//                    plate.setXtZxbz(DeletedEnum.NATURE.code);
//                    sysUserAppHomeconfigPlateMapper.insert(plate);
                    sysUserAppHomeconfigPlateMapper.updateById(userPlate.getId(), 0);
                }
            }
            if(!CollectionUtils.isEmpty(list)) {
                for(SysUserAppHomeconfigPlate plate : list) {
                    boolean bool = isExist(plates, plate.getPzId(), plate.getPlateId());
                    if(!bool) {
                        sysUserAppHomeconfigPlateMapper.updateById(plate.getId(), 1);
                    }
                }
            }

        }
        return true;
    }

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

    @Override
    public void updateInfoBatch(List<SysUserAppHomeconfigPlate> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            Long pzid = plates.get(0).getPzId();
            sysUserAppHomeconfigPlateMapper.deleteByPzid(pzid);
            for(SysUserAppHomeconfigPlate plate : plates) {
                plate.setXtCjsj(new Date());
                plate.setXtLrip(ipAddress);
                plate.setXtLrsj(new Date());
                plate.setXtZhxgsj(new Date());
                plate.setXtZxbz(DeletedEnum.NATURE.code);
                sysUserAppHomeconfigPlateMapper.insert(plate);
            }
        }
    }

    @Override
    public void delete(List<Long> list, String ipAddress, UserInfo userInfo) {
        if(!CollectionUtils.isEmpty(list)) {
            for(Long id : list) {
                SysUserAppHomeconfigPlate plate = sysUserAppHomeconfigPlateMapper.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());
                    }
                    sysUserAppHomeconfigPlateMapper.updateById(plate);
                }
            }
        }
    }

    @Override
    public IPage<SysUserAppHomeconfigPlate> selectByPage(SysUserAppHomeconfigPlatePage platePage) {
        QueryWrapper<SysUserAppHomeconfigPlate> queryWrapper = new QueryWrapper<>();
        if (platePage.getPzId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("pz_id", platePage.getPzId()));
        }
        if (platePage.getPlateId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("plate_id", platePage.getPlateId()));
        }
        if (StringUtils.isNotBlank(platePage.getBklx())) {
            queryWrapper.and(wrapper -> wrapper.eq("bklx", platePage.getBklx()));
        }
        if (StringUtils.isNotBlank(platePage.getBkmc())) {
            queryWrapper.and(wrapper -> wrapper.like("bkmc", platePage.getBkmc()));
        }
        queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
        List<OrderItem> orderBy = Lists.newArrayList(OrderItem.asc("id"));
        platePage.setOrders(orderBy);
        return sysUserAppHomeconfigPlateMapper.selectPage(platePage, queryWrapper);
    }

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

}
