package com.liquidnet.client.admin.zhengzai.smile.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.smile.dto.ShowSort;
import com.liquidnet.client.admin.zhengzai.smile.dto.ShowVo;
import com.liquidnet.client.admin.zhengzai.smile.dto.SmileShowParam;
import com.liquidnet.client.admin.zhengzai.smile.service.ISmileShowService;
import com.liquidnet.client.admin.zhengzai.smile.utils.SmileRedisUtils;
import com.liquidnet.service.goblin.constant.SmileRedisConst;
import com.liquidnet.service.kylin.dao.PerformanceOrderStatisticalDao;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinPerformanceVo;
import com.liquidnet.service.kylin.entity.KylinOrderTickets;
import com.liquidnet.service.kylin.entity.KylinPerformances;
import com.liquidnet.service.kylin.mapper.KylinOrderTicketRelationsMapper;
import com.liquidnet.service.kylin.mapper.KylinOrderTicketsMapper;
import com.liquidnet.service.kylin.mapper.KylinPerformancesMapper;
import com.liquidnet.service.kylin.mapper.KylinTicketStatusMapper;
import com.liquidnet.service.kylin.service.admin.IKylinPerformancesAdminService;
import com.liquidnet.service.smile.entity.SmileSchool;
import com.liquidnet.service.smile.mapper.SmileSchoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2022-03-14
 */
@Slf4j
@Service
public class SmileShowServiceImpl extends ServiceImpl<SmileSchoolMapper, SmileSchool> implements ISmileShowService {

    @Autowired
    KylinTicketStatusMapper ticketStatusMapper;

    @Autowired
    SmileRedisUtils smileRedisUtils;

    @Autowired
    private KylinPerformancesMapper performancesMapper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    IKylinPerformancesAdminService kylinPerformancesService;

    @Autowired
    KylinPerformancesMapper kylinPerformancesMapper;

    @Autowired
    private KylinOrderTicketRelationsMapper kylinOrderTicketRelationsMapper;

    @Autowired
    KylinOrderTicketsMapper kylinOrderTicketsMapper;

    @Override
    public List<ShowVo> listShow(SmileShowParam smileShowParam) {
        PageInfo<KylinPerformances> pageInfo;

        List<KylinPerformances>  kylinPerformancesList = kylinPerformancesMapper.selectPerIdByAgent();

        List<ShowVo> collect = kylinPerformancesList.stream().map(kylinPerformances -> {
            ShowVo vo = new ShowVo();
            vo.setPerformancesId(kylinPerformances.getPerformancesId());
            vo.setTitle(kylinPerformances.getTitle());
            //根据演出id查询订单  查询订单
            List<KylinOrderTickets> kylinOrderTicketsList = kylinOrderTicketsMapper.selectListByPerformanceId(kylinPerformances.getPerformancesId());

            //根据演出id查询查询销售数量
            vo.setOpen(kylinOrderTicketRelationsMapper.concatByAgent(kylinPerformances.getPerformancesId()));
            //总销售金额

            //订单

            //根据演出id查询销售代理
            vo.setOpen(kylinOrderTicketRelationsMapper.concatByAgentDed(kylinPerformances.getPerformancesId()));

            return vo;
        }).collect(Collectors.toList());


        //获取演出排列顺序  演出排序ID
        List<Object> list = smileRedisUtils.lGet(SmileRedisConst.SMILE_SHOW, 0, -1);






        //从redis 中查出开启代理的演出
        List<Object> performancesIds = smileRedisUtils.lGet(SmileRedisConst.SMILE_SHOW.concat(":performancesId"), 0, -1);
        //根据演出ID获取演出信息
        performancesIds.stream().map(performancesId -> {

            String id = performancesId.toString();
            //根据performancesId查询演出详情
            KylinPerformanceVo kylinPerformanceVo = mongoTemplate.findOne(Query.query(Criteria.where("performancesId").is(id)), KylinPerformanceVo.class, KylinPerformanceVo.class.getSimpleName());
            //查询销售统计
            List<PerformanceOrderStatisticalDao> result = kylinPerformancesService.getPerformanceOrderStatisticalList(id);


            //返回演出列表
            ShowVo showVo = new ShowVo();
            assert kylinPerformanceVo != null;
            showVo.setPerformancesId(id);
            showVo.setTitle(kylinPerformanceVo.getTitle());
            showVo.setTimeEnd(kylinPerformanceVo.getTimeEnd());
            showVo.setCityName(kylinPerformanceVo.getCityName());
            //演出顺序



            showVo.setOrderNum(null);
            //获取打款状态

            if (result.size() >= 2) {
                PerformanceOrderStatisticalDao resdao = result.get(result.size() - 1);
                showVo.setTotalAmount(resdao.getTotalSalePrice());

            }


            return showVo;
        }).collect(Collectors.toList());


        return null;
    }

    @Override
    public AjaxResult sort(ShowSort showSort) {
        //查询数据库是否又该顺序的演出
        kylinPerformancesMapper.updateComment(showSort.getSort());
        kylinPerformancesMapper.updateCommentByPer(showSort.getSort(),showSort.getPerformancesId());

        //获取排序演出id
        List<String> collect = kylinPerformancesMapper.selectByComment().stream().map(KylinPerformances::getPerformancesId).collect(Collectors.toList());
        String perId = StringUtils.strip(collect.toString(),"[]");
        smileRedisUtils.set(SmileRedisConst.SMILE_SHOW, perId);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult make(String performancesId) {
        if (!smileRedisUtils.hasKey(SmileRedisConst.SELL_SHOW_PAYMENT.concat(performancesId))){
            smileRedisUtils.set(SmileRedisConst.SELL_SHOW_PAYMENT.concat(performancesId),1);
        }else {
            smileRedisUtils.del(SmileRedisConst.SELL_SHOW_PAYMENT.concat(performancesId));
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getShowById(String performancesId) {

        return null;
    }


}
