jiangping
2023-10-26 519b464e4ed3d07bd5aa23a4067e5869058de2e5
server/src/main/java/doumeemes/service/business/impl/BizLingyangServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,245 @@
package doumeemes.service.business.impl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import doumeemes.biz.system.SystemDictDataBiz;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.DateUtil;
import doumeemes.core.utils.Utils;
import doumeemes.dao.business.BizLingyangMapper;
import doumeemes.dao.business.CompanyUserMapper;
import doumeemes.dao.business.model.BizLingyang;
import doumeemes.dao.business.model.Company;
import doumeemes.dao.business.model.CompanyUser;
import doumeemes.dao.system.join.SystemLoginLogJoinMapper;
import doumeemes.dao.system.model.SystemLoginLog;
import doumeemes.service.business.BizLingyangService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import doumeemes.service.business.CompanyUserService;
import doumeemes.service.ext.CompanyUserExtService;
import doumeemes.service.system.SystemLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
/**
 * ç¾šç¾Šå¯¹æŽ¥æ•°æ®æ˜Žç»†è¡¨Service实现
 * @author æ±Ÿè¹„蹄
 * @date 2023/10/25 18:17
 */
@Service
public class BizLingyangServiceImpl implements BizLingyangService {
    @Autowired
    private BizLingyangMapper bizLingyangMapper;
    @Autowired
    @Lazy
    private CompanyUserMapper companyUserMapper;
    @Autowired
    @Lazy
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private SystemLoginLogJoinMapper systemLoginLogJoinMapper;
    @Override
    public Integer create(BizLingyang bizLingyang) {
        bizLingyangMapper.insert(bizLingyang);
        return bizLingyang.getId();
    }
    @Override
    public void deleteById(Integer id) {
        bizLingyangMapper.deleteById(id);
    }
    @Override
    public void delete(BizLingyang bizLingyang) {
        UpdateWrapper<BizLingyang> deleteWrapper = new UpdateWrapper<>(bizLingyang);
        bizLingyangMapper.delete(deleteWrapper);
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        bizLingyangMapper.deleteBatchIds(ids);
    }
    @Override
    public void updateById(BizLingyang bizLingyang) {
        bizLingyangMapper.updateById(bizLingyang);
    }
    @Override
    public void updateByIdInBatch(List<BizLingyang> bizLingyangs) {
        if (CollectionUtils.isEmpty(bizLingyangs)) {
            return;
        }
        for (BizLingyang bizLingyang: bizLingyangs) {
            this.updateById(bizLingyang);
        }
    }
    @Override
    @Transactional
    public void   syncLingData(Date date) {
//        Date date = new Date();
        QueryWrapper<SystemLoginLog> param = new QueryWrapper<>();
        MPJLambdaWrapper<SystemLoginLog> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(SystemLoginLog.class);
        mpjLambdaWrapper.leftJoin(Company.class,Company::getId,SystemLoginLog::getCompanyId);
        mpjLambdaWrapper.leftJoin(CompanyUser.class,CompanyUser::getId,SystemLoginLog::getCompanyUserId);
        mpjLambdaWrapper.select("(select count(b.id) from system_trace_log b where to_days(b.OPERA_TIME) =to_days('"+ DateUtil.getPlusTime2(date) +"') and b.company_id=t1.id ) as logNum");
        mpjLambdaWrapper.selectAs( CompanyUser::getLingyangUserid,SystemLoginLog::getLingyangUserid);
        mpjLambdaWrapper.selectAs( CompanyUser::getRootDepartId,SystemLoginLog::getRootDepartId);
        mpjLambdaWrapper.selectAs(Company::getCreditCode, SystemLoginLog::getCompanyCode);
        mpjLambdaWrapper.eq(SystemLoginLog::getSuccess, Constants.ONE);
        mpjLambdaWrapper.eq(SystemLoginLog::getOrgin, Constants.USER_LOGIN_ORIGIN.lingyang);
        mpjLambdaWrapper.apply("to_days(login_time) =to_days('"+ DateUtil.getPlusTime2(date) +"')" );
        mpjLambdaWrapper.select("count(t.id) as num");
        mpjLambdaWrapper.groupBy(SystemLoginLog::getCompanyId);
        List<SystemLoginLog> list = systemLoginLogJoinMapper.selectJoinList(SystemLoginLog.class,mpjLambdaWrapper);
        if(list!=null && list.size()>0){
            //删除老的
            bizLingyangMapper.delete(new QueryWrapper<BizLingyang>()
                    .lambda().apply("to_days(create_time) =to_days('"+ DateUtil.getPlusTime2(date) +"')" )
            );
            for(SystemLoginLog log : list){
                CompanyUser user = new CompanyUser();
                user.setDeleted(Constants.ZERO);
                user.setRootDepartId(log.getRootDepartId());
                user = companyUserMapper.selectOne(new QueryWrapper<>(user).lambda().orderByDesc(CompanyUser::getIsMaster).last("limit 1" ));
                BizLingyang model = new BizLingyang();
                model.setCompanyId(log.getCompanyId());
                model.setCreateTime(date);
                model.setUserPhone(user !=null? user.getPhone():log.getLoginUsername());
                model.setCreditCode(log.getCompanyCode());
                try {
                    model.setUserId(user !=null?Long.parseLong(user.getLingyangUserid()):Long.parseLong(log.getLingyangUserid()));
                }catch (Exception e){
                }
                model.setAppKey(systemDictDataBiz.queryByCode(Constants.LINGYANG_PARAM,Constants.LINGYANG_APPKEY ).getCode());
                model.setActionTimes(log.getNum() + log.getLogNum());
                model.setLoginTimes(log.getNum());
                bizLingyangMapper.insert(model);
            }
        }
    }
//    @Override
    @Transactional
    public void   syncLingDataGroupByUser(Date date) {
//        Date date = new Date();
        QueryWrapper<SystemLoginLog> param = new QueryWrapper<>();
        MPJLambdaWrapper<SystemLoginLog> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(SystemLoginLog.class);
        mpjLambdaWrapper.leftJoin(Company.class,Company::getId,SystemLoginLog::getCompanyId);
        mpjLambdaWrapper.leftJoin(CompanyUser.class,CompanyUser::getId,SystemLoginLog::getCompanyUserId);
        mpjLambdaWrapper.select("(select count(b.id) from system_trace_log b where to_days(b.OPERA_TIME) =to_days('"+ DateUtil.getPlusTime2(date) +"') and b.company_id=t1.id and b.company_user_id=t2.id) as logNum");
        mpjLambdaWrapper.selectAs( CompanyUser::getLingyangUserid,SystemLoginLog::getLingyangUserid);
        mpjLambdaWrapper.selectAs(Company::getCreditCode, SystemLoginLog::getCompanyCode);
        mpjLambdaWrapper.eq(SystemLoginLog::getSuccess, Constants.ONE);
        mpjLambdaWrapper.eq(SystemLoginLog::getOrgin, Constants.USER_LOGIN_ORIGIN.lingyang);
        mpjLambdaWrapper.apply("to_days(login_time) =to_days('"+ DateUtil.getPlusTime2(date) +"')" );
        mpjLambdaWrapper.select("count(t.id) as num");
        mpjLambdaWrapper.groupBy(SystemLoginLog::getUserId);
        List<SystemLoginLog> list = systemLoginLogJoinMapper.selectJoinList(SystemLoginLog.class,mpjLambdaWrapper);
        if(list!=null && list.size()>0){
            //删除老的
            bizLingyangMapper.delete(new QueryWrapper<BizLingyang>()
                    .lambda().apply("to_days(create_time) =to_days('"+ DateUtil.getPlusTime2(date) +"')" )
            );
            for(SystemLoginLog log : list){
                BizLingyang model = new BizLingyang();
                model.setCompanyId(log.getCompanyId());
                model.setCreateTime(date);
                model.setUserPhone(log.getLoginUsername());
                model.setCreditCode(log.getCompanyCode());
                try {
                    model.setUserId(Long.parseLong(log.getLingyangUserid()));
                }catch (Exception e){
                }
                model.setAppKey(systemDictDataBiz.queryByCode(Constants.LINGYANG_PARAM,Constants.LINGYANG_APPKEY ).getCode());
                model.setActionTimes(log.getNum() + log.getLogNum());
                model.setLoginTimes(log.getNum());
                bizLingyangMapper.insert(model);
            }
        }
    }
    @Override
    public BizLingyang findById(Integer id) {
        return bizLingyangMapper.selectById(id);
    }
    @Override
    public BizLingyang findOne(BizLingyang bizLingyang) {
        QueryWrapper<BizLingyang> wrapper = new QueryWrapper<>(bizLingyang);
        return bizLingyangMapper.selectOne(wrapper);
    }
    @Override
    public List<BizLingyang> findList(BizLingyang bizLingyang) {
        QueryWrapper<BizLingyang> wrapper = new QueryWrapper<>(bizLingyang);
        return bizLingyangMapper.selectList(wrapper);
    }
    @Override
    public PageData<BizLingyang> findPage(PageWrap<BizLingyang> pageWrap) {
        IPage<BizLingyang> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<BizLingyang> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(BizLingyang::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getRemark() != null) {
            queryWrapper.lambda().eq(BizLingyang::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getCompanyId() != null) {
            queryWrapper.lambda().eq(BizLingyang::getCompanyId, pageWrap.getModel().getCompanyId());
        }
        if (pageWrap.getModel().getCreditCode() != null) {
            queryWrapper.lambda().eq(BizLingyang::getCreditCode, pageWrap.getModel().getCreditCode());
        }
        if (pageWrap.getModel().getAppKey() != null) {
            queryWrapper.lambda().eq(BizLingyang::getAppKey, pageWrap.getModel().getAppKey());
        }
        if (pageWrap.getModel().getUserPhone() != null) {
            queryWrapper.lambda().eq(BizLingyang::getUserPhone, pageWrap.getModel().getUserPhone());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(BizLingyang::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getActionTimes() != null) {
            queryWrapper.lambda().eq(BizLingyang::getActionTimes, pageWrap.getModel().getActionTimes());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(bizLingyangMapper.selectPage(page, queryWrapper));
    }
    @Override
    public long count(BizLingyang bizLingyang) {
        QueryWrapper<BizLingyang> wrapper = new QueryWrapper<>(bizLingyang);
        return bizLingyangMapper.selectCount(wrapper);
    }
}