package com.yida.data.school.transaction.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.apply.EduProduct;
import com.yida.data.common.core.entity.apply.EduProductNotice;
import com.yida.data.common.core.entity.apply.EduProductNoticeStudent;
import com.yida.data.common.core.entity.constant.AppConstant;
import com.yida.data.common.core.entity.notice.qywx.TextSchoolNotice;
import com.yida.data.common.core.entity.notice.qywx.inside.Text;
import com.yida.data.common.core.entity.system.EduApp;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.utils.WxUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.job.feign.RemoteJobService;
import com.yida.data.school.dto.transaction.ListBuyStudentDTO;
import com.yida.data.school.transaction.mapper.EduProductMapper;
import com.yida.data.school.transaction.mapper.EduProductNoticeMapper;
import com.yida.data.school.transaction.service.EduProductApplyService;
import com.yida.data.school.transaction.service.EduProductNoticeService;
import com.yida.data.school.transaction.service.EduProductNoticeStudentService;
import com.yida.data.school.transaction.service.EduProductService;
import com.yida.data.school.vo.transcation.EduProductNoticeVO;
import com.yida.data.user.feign.RemoteStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品催缴通知策略表 Service实现
 *
 * @author ccl
 * @date 2021-09-01 18:23:14
 */
@RefreshScope
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduProductNoticeServiceImpl extends ServiceImpl
        <EduProductNoticeMapper, EduProductNotice> implements EduProductNoticeService {

    private final EduProductNoticeMapper eduProductNoticeMapper;


    @Resource
    private RemoteJobService remoteJobService;
    @Resource
    private EduProductApplyService eduProductApplyService;
    @Resource
    private WxUtil wxUtil;
    @Resource
    private CommonService commonService;
    @Resource
    private EduProductService eduProductService;
    @Resource
    private EduProductMapper eduProductMapper;
    @Resource
    private RemoteStudentService remoteStudentService;
    @Resource
    private EduProductNoticeStudentService eduProductNoticeStudentService;
    @Value("${febs.pageUrl}")
    private String pageUrl;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertNotice(List<EduProductNotice> notices) {

        saveOrUpdateBatch(notices);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void productSendNotice(Long id) {
        EduProductNotice notice = getById(id);
        EduProduct product = eduProductService.getById(notice.getProductId());

        //催缴通知,给全校发
        EduApp app = commonService.getAppByCodeAndSchool(AppConstant.INDEX_PARENT, null,
                product.getSchoolId());
        String accessToken = wxUtil.getAccessToken(app.getWxCorpId(), app.getWxSecret());
        Text text = new Text();

        String url = pageUrl + "/campusService/index.html?" + "&corpId=" + app.getWxCorpId() + "&detail=" + product.getProductId();
        //a标签
        String content = notice.getContent() + "\n" + "<a href='" + url + "'>" + "#点击此处前往缴费#" + "</a>";
        text.setContent(content);
        TextSchoolNotice textSchoolNotice = new TextSchoolNotice();
        textSchoolNotice.setText(text);
        textSchoolNotice.setAgentid(app.getWxAgentId());

        ListBuyStudentDTO listBuyStudentDTO = new ListBuyStudentDTO();
        listBuyStudentDTO.setProductId(product.getProductId());
        listBuyStudentDTO.setSchoolId(product.getSchoolId());
        List<Long> studentIds = new ArrayList<>();
        //需要新增的通知记录
        ArrayList<EduProductNoticeStudent> record = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        //如果putNum不是空，查询出来为快到期的人，并且为续费通知
        if (notice.getPutNum() != null) {
            listBuyStudentDTO.setDateTime(LocalDateTime.now().plusDays(notice.getPutNum()));
            List<EduProductNoticeVO> list = eduProductMapper.listBuyStudent(listBuyStudentDTO);
            for (EduProductNoticeVO o : list) {
                //快到期学生
                studentIds.add(o.getStudentId());

            }
            listBuyStudentDTO.setStudentId(studentIds);
            List<EduStudent> students = remoteStudentService.listAllNotBuyStudent(listBuyStudentDTO).getData();
            studentIds = new ArrayList<>();
            for (EduStudent student : students) {
                studentIds.add(student.getId());
                //新增通知的具体元素
                EduProductNoticeStudent eduProductNoticeStudent = new EduProductNoticeStudent();
                eduProductNoticeStudent.setNoticeId(id);
                //自动推送
                eduProductNoticeStudent.setPutType(0);
                //快到期
                eduProductNoticeStudent.setReceiveType(1);
                eduProductNoticeStudent.setProductId(product.getProductId());
                eduProductNoticeStudent.setStudentId(student.getId());
                eduProductNoticeStudent.setContent(notice.getContent());
                eduProductNoticeStudent.setCreateDate(now);
                record.add(eduProductNoticeStudent);
            }

        } else {
            //首购通知
            List<EduProductNoticeVO> list = eduProductMapper.listBuyStudent(listBuyStudentDTO);
            for (EduProductNoticeVO o : list) {
                studentIds.add(o.getStudentId());
            }
            listBuyStudentDTO.setStudentId(studentIds);
            List<EduStudent> students = remoteStudentService.listAllNotBuyStudent(listBuyStudentDTO).getData();
            //清空list，放入未购买学生
            studentIds = new ArrayList<>();
            for (EduStudent student : students) {
                studentIds.add(student.getId());
                //新增通知的具体元素
                EduProductNoticeStudent eduProductNoticeStudent = new EduProductNoticeStudent();
                eduProductNoticeStudent.setNoticeId(id);
                //自动推送
                eduProductNoticeStudent.setPutType(0);
                //首购通知
                eduProductNoticeStudent.setProductId(product.getProductId());
                eduProductNoticeStudent.setReceiveType(0);
                eduProductNoticeStudent.setStudentId(student.getId());
                eduProductNoticeStudent.setContent(notice.getContent());
                eduProductNoticeStudent.setCreateDate(now);
                record.add(eduProductNoticeStudent);
            }


        }
        //家长微信id
        List<String> parentWxId = remoteStudentService.findParentWxId(studentIds).getData();
        textSchoolNotice.setTo_parent_userid(parentWxId);
        //发送微信通知
        wxUtil.pushSchoolNotice(accessToken, textSchoolNotice);
        //新增通知记录
        eduProductNoticeStudentService.saveBatch(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void productMonitor() {

        eduProductMapper.productMonitor();
    }
}
