LiveApplyServiceImpl.java 13.7 KB
package com.cnlive.shenhe.service.serviceImpl;


import com.alibaba.fastjson.JSON;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.dto.LiveApplyDTO;
import com.cnlive.shenhe.entity.ShyLiveapply;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShyLiveapplyMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.service.LiveApplyService;
import com.cnlive.shenhe.service.SitesService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.List;

/**
 * @Auther: liwei
 * @Date: 2018/11/30 14:23
 * @Description:
 */
@Service
public class LiveApplyServiceImpl implements LiveApplyService {

    private static Logger logger = LoggerFactory.getLogger(LiveApplyServiceImpl.class);
    @Autowired
    ShyLiveapplyMapper shyLiveApplyMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    @Autowired
    SitesService sitesService;

    /**
     * 导入直播申请
     *
     * @param shyLiveApply
     * @return
     */
    @Override
    public boolean impotLiveApply(ShyLiveapply shyLiveApply) {
        int result = shyLiveApplyMapper.insertSelective(shyLiveApply);
        return result != 0;
    }

    /**
     * 根据主键id查询直播申请对象
     *
     * @param id
     * @return
     */
    @Override
    public ShyLiveapply selectByPrimaryKey(Integer id) {
        return shyLiveApplyMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据条件查询直播申请集合
     *
     * @param shyLiveApply
     * @return
     */
    @Override
    public List<ShyLiveapply> findShyLiveApply(ShyLiveapply shyLiveApply) {
        return shyLiveApplyMapper.selectByRowBounds(shyLiveApply, new RowBounds(0, 100));
    }

    /**
     * 修改直播申请对象  返回布尔类型
     *
     * @param shyLiveApply
     * @return
     */
    @Override
    public boolean updateShyLiveApply(ShyLiveapply shyLiveApply) {
        int i = shyLiveApplyMapper.updateByPrimaryKey(shyLiveApply);
        return i != 0;
    }

    /**
     * 修改直播申请对象 返回int类型
     *
     * @param id
     * @param auditorId
     * @param updater
     * @param status
     * @param msg
     * @return
     */
    @Override
    public int updateLiveApply(Integer id, String auditorId, String updater, Integer status, String msg) {
        ShyLiveapply shyLiveapply = shyLiveApplyMapper.selectByPrimaryKey(id);
        shyLiveapply.setAuditor(auditorId);
        shyLiveapply.setShenhe_state(status);
        shyLiveapply.setReceive(CommonConst.RECEIVE_AFTER);
        shyLiveapply.setShenhe_msg(msg);
        shyLiveapply.setUpdater(updater);
        shyLiveapply.setUpdated_at(CommonUtils.getCurrentTime());
        return shyLiveApplyMapper.updateByPrimaryKey(shyLiveapply);
    }

    /**
     * 获取分页、条件查询
     *
     * @param liveApplyDTO
     * @return
     */
    @Override
    public PageInfo<ShyLiveapply> getListLiveApply(LiveApplyDTO liveApplyDTO) {
        logger.info("接参数列表为,LiveApplyDTO:{}", JSON.toJSONString(liveApplyDTO));
        Example example = new Example(ShyLiveapply.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(liveApplyDTO.getOrderByClause())) {
            example.setOrderByClause(liveApplyDTO.getOrderByClause());
        } else {
            example.setOrderByClause("created_at desc");
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getStart_date())) {
            Timestamp startdate = null;
            try {
                startdate = CommonUtils.parseDate(liveApplyDTO.getStart_date(), "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andGreaterThanOrEqualTo("pre_start_time", startdate);
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getEnd_date())) {
            Timestamp enddate = null;
            try {
                enddate = CommonUtils.parseDate(liveApplyDTO.getEnd_date(), "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andLessThanOrEqualTo("pre_end_time", enddate);
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getTheme())) {
            criteria.andLike("theme", "%" + liveApplyDTO.getTheme() + "%");
        }

        if (CommonUtils.isNotEmpty(liveApplyDTO.getOrganization())) {
            criteria.andLike("organization", "%" + liveApplyDTO.getOrganization() + "%");
        }
        if (CommonUtils.isNotNull(liveApplyDTO.getSpid())) {
            criteria.andEqualTo("sp_id", liveApplyDTO.getSpid());
        }
        //不是   我的已审 进来的,只查一种状态
        if (CommonUtils.isNotNull(liveApplyDTO.getShenhe_state()) && liveApplyDTO.getShenhe_state() != 3) {
            criteria.andEqualTo("shenhe_state", liveApplyDTO.getShenhe_state());
            //是 我的已审,查通过和拒绝的两种状态
        } else if (CommonUtils.isNotNull(liveApplyDTO.getShenhe_state()) && liveApplyDTO.getShenhe_state() == 3) {
            criteria.andBetween("shenhe_state", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }
        if (CommonUtils.isNotNull(liveApplyDTO.getReceive())) {
            criteria.andEqualTo("receive", liveApplyDTO.getReceive());
            if (liveApplyDTO.getReceive() != 0 && CommonUtils.isNotEmpty(liveApplyDTO.getUserId())) {
                criteria.andEqualTo("auditor", liveApplyDTO.getUserId());
            }
        }
        PageHelper.startPage(liveApplyDTO.getPage(), liveApplyDTO.getPageSize(), true);
        List<ShyLiveapply> shyLiveApplyList = shyLiveApplyMapper.selectByExample(example);
        PageInfo<ShyLiveapply> pageInfo = new PageInfo<>(shyLiveApplyList);
        return pageInfo;
    }

    /**
     * 获取直播申请详情
     *
     * @param id
     * @return
     */
    @Override
    public ShyLiveapply getDetailsLiveApply(Integer id) {
        ShyLiveapply shyLiveapply = shyLiveApplyMapper.selectByPrimaryKey(id);
        if (!shyLiveapply.getShenhe_state().equals(CommonConst.RECEIVE_BEFORE)) {
            String updater = CommonUtils.isEmpty(shyLiveapply.getUpdater()) || "null".equals(shyLiveapply.getUpdater()) ? "自动通过" : shyLiveapply.getUpdater();
            String auditorId = shyLiveapply.getAuditor();
            if (CommonUtils.isNotEmpty(auditorId)) {
                ShyUsers user = new ShyUsers();
                user.setUser_id(auditorId);
                user = shyUsersMapper.selectOne(user);
                String email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
                updater = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
            }
            shyLiveapply.setUpdater(updater);
        }
        return shyLiveapply;
    }

    /**
     * 认领
     *
     * @param ids
     * @param num
     * @param userId
     * @param spid
     * @return
     */
    @Override
    public Integer receive(String ids, Integer num, String userId, Integer spid) {
        Integer n = 0;
        if (CommonUtils.isNotEmpty(ids)) {
            String[] cids = ids.split(",");
            for (String cid : cids) {
                ShyLiveapply shyLiveapply = shyLiveApplyMapper.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyLiveapply) && shyLiveapply.getShenhe_state().equals(CommonConst.RECEIVE_BEFORE)) {
                    shyLiveapply.setAuditor(userId);
                    ;
                    shyLiveapply.setShenhe_state(CommonConst.AUDIT_INTT);
                    shyLiveapply.setReceive(CommonConst.RECEIVE_AFTER);
                    shyLiveApplyMapper.updateByPrimaryKeySelective(shyLiveapply);
                    n++;
                }
            }
        } else if (CommonUtils.isNotNull(num) && num > 0) {
            Example example = new Example(ShyLiveapply.class);
            example.setOrderByClause("pre_start_time asc");
            example.and().andEqualTo("shenhe_state", CommonConst.AUDIT_INTT);
            example.and().andEqualTo("receive", CommonConst.RECEIVE_BEFORE);
            if (CommonUtils.isNotNull(spid) && spid != 0) {
                example.and().andEqualTo("spid", spid);
            }
            List<ShyLiveapply> shyLiveapplyList = shyLiveApplyMapper.selectByExampleAndRowBounds(example, new RowBounds(0, num));
            if (!shyLiveapplyList.isEmpty()) {
                for (ShyLiveapply shyLiveapply : shyLiveapplyList) {
                    shyLiveapply.setAuditor(userId);
                    shyLiveapply.setShenhe_state(CommonConst.AUDIT_INTT);
                    shyLiveapply.setReceive(CommonConst.RECEIVE_AFTER);
                    shyLiveApplyMapper.updateByPrimaryKeySelective(shyLiveapply);
                    n++;
                }
            }
        }
        return n;
    }

    /**
     * 退领
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public ResponseBean retReceive(String ids, String userId) {
        String[] cids = ids.split(",");
        for (String cid : cids) {
            ShyLiveapply shyLiveapply = shyLiveApplyMapper.selectByPrimaryKey(Integer.parseInt(cid));
            if (shyLiveapply.getShenhe_state().equals(CommonConst.AUDIT_INTT) && shyLiveapply.getReceive().equals(CommonConst.RECEIVE_AFTER) && shyLiveapply.getAuditor().equals(userId)) {
                shyLiveapply.setShenhe_state(CommonConst.AUDIT_INTT);
                shyLiveapply.setReceive(CommonConst.RECEIVE_BEFORE);
                shyLiveapply.setAuditor(null);
                int i = shyLiveApplyMapper.updateByPrimaryKey(shyLiveapply);
                if (i != 1) {
                    return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "退领失败,请重试");
                }
            } else {
                return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "退领失败,您选中的内容包含已退领的或已经审核的");
            }
        }
        return new ResponseBean(ErrorEnum.SUCCESS.getErrorCode(), "已退领");
    }

    /**
     * 根据条件导出数据excel
     *
     * @return
     */
    @Override
    public List<ShyLiveapply> findByCondition(LiveApplyDTO liveApplyDTO) {
        Example example = new Example(ShyLiveapply.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(liveApplyDTO.getOrderByClause())) {
            example.setOrderByClause(liveApplyDTO.getOrderByClause());
        } else {
            example.setOrderByClause("pre_start_time");
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getStart_date())) {
            Timestamp startdate = null;
            try {
                startdate = CommonUtils.parseDate(liveApplyDTO.getStart_date(), "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andGreaterThanOrEqualTo("pre_start_time", startdate);
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getEnd_date())) {
            Timestamp enddate = null;
            try {
                enddate = CommonUtils.parseDate(liveApplyDTO.getEnd_date(), "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andLessThanOrEqualTo("pre_end_time", enddate);
        }
        if (CommonUtils.isNotEmpty(liveApplyDTO.getTheme())) {
            criteria.andLike("theme", "%" + liveApplyDTO.getTheme() + "%");
        }

        if (CommonUtils.isNotNull(liveApplyDTO.getBusiness_model())) {
            criteria.andEqualTo("business_model", liveApplyDTO.getBusiness_model());
        }
        if (CommonUtils.isNotNull(liveApplyDTO.getSpid())) {
            criteria.andEqualTo("sp_id", liveApplyDTO.getSpid());
        }
        //不是   我的已审 进来的,只查一种状态
        if (CommonUtils.isNotNull(liveApplyDTO.getShenhe_state()) && liveApplyDTO.getShenhe_state() != 3) {
            criteria.andEqualTo("shenhe_state", liveApplyDTO.getShenhe_state());
            //是 我的已审,查通过和拒绝的两种状态
        } else if (CommonUtils.isNotNull(liveApplyDTO.getShenhe_state()) && liveApplyDTO.getShenhe_state() == 3) {
            criteria.andBetween("shenhe_state", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }
        if (CommonUtils.isNotNull(liveApplyDTO.getReceive())) {
            criteria.andEqualTo("receive", liveApplyDTO.getReceive());
            if (liveApplyDTO.getReceive() != 0 && CommonUtils.isNotEmpty(liveApplyDTO.getUserId())) {
                criteria.andEqualTo("auditor", liveApplyDTO.getUserId());
            }
        }
        return shyLiveApplyMapper.selectByExample(example);
    }

    /**
     * 获得审核类型
     *
     * @param shyLiveApply
     */
    @Override
    public void getShenheType(ShyLiveapply shyLiveApply) {

        //获取直播频道免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_LIVEAPPLY);
        //初始化一个审核状态
        //审核类型,1:免审,2:机审,3:人工审
        Integer shenheType = CommonConst.AUDIT_TYPE_USER;
        //如果当前sp是免审sp
        if (sites.contains(shyLiveApply.getSp_id())) {
            //免审
            shenheType = CommonConst.AUDIT_TYPE_FREE;
        }
        shyLiveApply.setShenhe_type(shenheType);
    }
}