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.SysAppHomeconfigPlateList;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigPlateListPage;
import com.mosty.common.core.business.mapper.SysAppHomeconfigPlateListMapper;
import com.mosty.common.core.business.service.SysAppHomeconfigPlateListService;
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 SysAppHomeconfigPlateListServiceImpl extends ServiceImpl<SysAppHomeconfigPlateListMapper, SysAppHomeconfigPlateList> implements SysAppHomeconfigPlateListService {

    @Resource
    private SysAppHomeconfigPlateListMapper sysAppHomeconfigPlateListMapper;

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

    @Override
    public boolean saveInfoBatch(List<SysAppHomeconfigPlateList> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            for(SysAppHomeconfigPlateList plate : plates) {
                plate.setXtZhxgsj(new Date());
                plate.setXtCjsj(new Date());
                plate.setXtLrip(ipAddress);
                plate.setXtLrsj(new Date());
                plate.setXtZhxgsj(new Date());
                plate.setXtZxbz(DeletedEnum.NATURE.code);
                sysAppHomeconfigPlateListMapper.insert(plate);
            }
        }
        return true;
    }

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

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

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

    @Override
    public IPage<SysAppHomeconfigPlateList> selectByPage(SysAppHomeconfigPlateListPage platePage) {
        QueryWrapper<SysAppHomeconfigPlateList> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(platePage.getNrmc())) {
            queryWrapper.and(wrapper -> wrapper.like("nrmc", platePage.getNrmc()));
        }
        if(platePage.getPlateId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq("pz_id", platePage.getPlateId()));
        }
        queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
        List<OrderItem> orderBy = Lists.newArrayList(OrderItem.asc("id"));
        platePage.setOrders(orderBy);
        return sysAppHomeconfigPlateListMapper.selectPage(platePage, queryWrapper);
    }
}
