package com.liquidnet.service.kylin.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.commons.lang.util.BeanUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.kylin.dao.PerformanceAdminListDao;
import com.liquidnet.service.kylin.dao.RoadShowAdminListDao;
import com.liquidnet.service.kylin.dto.param.CreateRoadShowParam;
import com.liquidnet.service.kylin.dto.vo.RoadShowVo;
import com.liquidnet.service.kylin.dto.vo.admin.RoadShowAdminVo;
import com.liquidnet.service.kylin.entity.KylinRoadShows;
import com.liquidnet.service.kylin.mapper.KylinRoadShowsMapper;
import com.liquidnet.service.kylin.service.IKylinRoadShowsService;
import com.liquidnet.service.kylin.service.admin.IKylinRoadShowsAdminService;
import com.mongodb.BasicDBObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 巡演 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-11
 */
@Service
public class KylinRoadShowsAdminServiceImpl extends ServiceImpl<KylinRoadShowsMapper, KylinRoadShows> implements IKylinRoadShowsAdminService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoConverter mongoConverter;

    @Autowired
    private KylinRoadShowsMapper kylinRoadShowsMapper;

    @Override
    public String createRoadShow(CreateRoadShowParam createRoadShowParam) {
        try {
            String roadShowId = IDGenerator.nextSnowId().toString();
            createRoadShowParam.setStatus(1);
            createRoadShowParam.setRoadShowsId(roadShowId);
            createRoadShowParam.setCreatedAt(LocalDateTime.now());
            KylinRoadShows roadShows = new KylinRoadShows();
            BeanUtils.copyProperties(createRoadShowParam, roadShows);
            kylinRoadShowsMapper.insert(roadShows);
            RoadShowVo roadShowVo = new RoadShowVo();
            BeanUtils.copyProperties(createRoadShowParam, roadShowVo);
            mongoTemplate.insert(roadShows, RoadShowVo.class.getSimpleName());
            return roadShowId;
        } catch (Exception e) {
            return "";
        }
    }

    @Override
    public boolean updateRoadShow(CreateRoadShowParam createRoadShowParam) {
        try {
            KylinRoadShows roadShows = new KylinRoadShows();
            createRoadShowParam.setUpdatedAt(LocalDateTime.now());
            createRoadShowParam.setStatus(null);
            BeanUtils.copyProperties(createRoadShowParam, roadShows);
            kylinRoadShowsMapper.update(roadShows, new UpdateWrapper<KylinRoadShows>().eq("road_shows_id", createRoadShowParam.getRoadShowsId()));
            RoadShowVo roadShowVo = new RoadShowVo();
            BeanUtils.copyProperties(createRoadShowParam, roadShowVo);

            BasicDBObject objectRoadShowVo = new BasicDBObject("$set", mongoConverter.convertToMongoType(roadShows));
            mongoTemplate.getCollection(KylinRoadShows.class.getSimpleName()).updateOne(
                    Query.query(Criteria.where("roadShowsId").is(createRoadShowParam.getRoadShowsId())).getQueryObject(),
                    objectRoadShowVo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteRoadShow(String roadShowId, int status) {

        KylinRoadShows roadShows = new KylinRoadShows();
        roadShows.setStatus(status);
        roadShows.setUpdatedAt(LocalDateTime.now());
        kylinRoadShowsMapper.update(roadShows, new UpdateWrapper<KylinRoadShows>().eq("road_shows_id", roadShowId));

        BasicDBObject objectRoadShowVo = new BasicDBObject("$set", mongoConverter.convertToMongoType(roadShows));
        mongoTemplate.getCollection(KylinRoadShows.class.getSimpleName()).updateOne(
                Query.query(Criteria.where("roadShowsId").is(roadShowId)).getQueryObject(),
                objectRoadShowVo);

        return false;
    }

    @Override
    public RoadShowAdminVo detailsRoadShow(String roadShowId) {
        KylinRoadShows data = kylinRoadShowsMapper.selectOne(new UpdateWrapper<KylinRoadShows>().eq("road_shows_id", roadShowId));
        RoadShowAdminVo vo = new RoadShowAdminVo();
        vo.setTitle(data.getTitle());
        vo.setRoadShowId(data.getRoadShowsId());
        vo.setStatus(data.getStatus());
        return vo;
    }

    @Override
    public PageInfo<RoadShowAdminListDao> listRoadShow(String title, int page, int size) {
        PageInfo<RoadShowAdminListDao> pageInfoTmp = null;
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title", title);
            PageHelper.startPage(page, size);
            List<RoadShowAdminListDao> voList = kylinRoadShowsMapper.misRoadShowList(map);
            pageInfoTmp = new PageInfo(voList);
        } catch (Exception e) {
            return null;
        }
        return pageInfoTmp;
    }
}
