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.request.SysAppHomeconfigPlatePage;
import com.mosty.common.core.business.mapper.SysAppHomeconfigPlateMapper;
import com.mosty.common.core.business.service.SysAppHomeconfigPlateService;
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 SysAppHomeconfigPlateServiceImpl extends ServiceImpl<SysAppHomeconfigPlateMapper, SysAppHomeconfigPlate> implements SysAppHomeconfigPlateService {

    @Resource
    private SysAppHomeconfigPlateMapper sysAppHomeconfigPlateMapper;

    @Override
    public Long saveInfo(SysAppHomeconfigPlate 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);
        sysAppHomeconfigPlateMapper.insert(plate);
        return plate.getId();
    }

    @Override
    public boolean saveInfoBatch(List<SysAppHomeconfigPlate> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            for(SysAppHomeconfigPlate 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);
                sysAppHomeconfigPlateMapper.insert(plate);
            }
        }
        return true;
    }

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

    @Override
    public void updateInfoBatch(List<SysAppHomeconfigPlate> plates, String ipAddress) {
        if(!CollectionUtils.isEmpty(plates)) {
            Long homeId = plates.get(0).getHomeid();
            sysAppHomeconfigPlateMapper.deleteByHomeid(homeId);
            for(SysAppHomeconfigPlate 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);
                sysAppHomeconfigPlateMapper.insert(plate);
            }
        }
    }

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

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

}
