package com.ssj.service.weixin.library.service.impl;

import com.ssj.bean.weixin.libmy.domain.LibReadVip;
import com.ssj.bean.weixin.libmy.domain.TbLibUserApply;
import com.ssj.bean.weixin.push.domain.Miniprogram;
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
import com.ssj.dao.weixin.library.dao.ILibUserApplyDao;
import com.ssj.dao.weixin.library.dao.IReadParentQueryDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.weixin.util.Collections3;
import com.ssj.framework.weixin.util.WxUtil;
import com.ssj.service.weixin.library.dto.in.BookApplyInDto;
import com.ssj.service.weixin.library.dto.in.BookDetailDto;
import com.ssj.service.weixin.library.service.IBookDetailService;
import com.ssj.service.weixin.library.service.ILibUserApplyService;
import com.ssj.service.weixin.library.service.IReadVipService;
import com.ssj.service.weixin.push.service.PushTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class LibUserApplyServiceImpl extends BaseServiceImpl<TbLibUserApply,String> implements ILibUserApplyService {

    @Autowired
    private ILibUserApplyDao dao;

    @Autowired
    private IReadParentQueryDao queryDao;

    @Autowired
    private IReadVipService vipService;

    @Autowired
    private PushTemplateService pushTemplateService;
    @Autowired
    private IBookDetailService detailService;

    @Override
    public PagingAndSortingRepository<TbLibUserApply, String> getDao() {
        return dao;
    }

    @Override
    public void saveApplyBook(BookApplyInDto req, String userId) throws Exception{
        TbLibUserApply applyMain = new TbLibUserApply();
        Date now = new Date();

        applyMain.setLibId(req.getLibId());
        applyMain.setVipId(req.getVipId());
        applyMain.setUserId(userId);
        applyMain.setType(1);
        applyMain.setCreateTime(now);
        applyMain.setState(1);
        applyMain = this.save(applyMain);

        List<BookDetailDto> detailList = req.getDetailList();
        if(!detailList.isEmpty()){
           for(BookDetailDto dto :detailList){
               TbLibUserApply applyDetail = new TbLibUserApply();
               BeanUtils.copyProperties(dto,applyDetail);
               applyDetail.setPid(applyMain.getId());
               applyDetail.setType(2);
               applyDetail.setCreateTime(now);
               applyDetail.setUserId(userId);
               this.save(applyDetail);
           }
        }
        //推送消息到馆长
        String template_id_short="OPENTM207273120";
        Map<String, String> data = new HashMap<>(4);
        LibReadVip vip = vipService.getById(req.getVipId());
        data.put("first", "亲爱的馆长，"+vip.getChildName()+"家长在找好书小程序提交了新书申请需求，请确认");
        data.put("keyword1", "新书申请");
        data.put("keyword2", "请您确认");
        data.put("keyword3", DateHelper.format(new Date(), "yyyy年MM月dd日  HH时mm分 "));
        Miniprogram min = new Miniprogram();
        min.setPagepath("president/pages/newbooks_apply_record/newbooks_apply_record?id="+applyMain.getId());
        min.setAppid(WxUtil.map.get("readingAppId"));
        
        List<Map<String,Object>> list = detailService.libManageList(req.getLibId());
        if(Collections3.isNotEmpty(list)){
            for (Map<String,Object> map :list){
                SendTemplateShort templateShort = new SendTemplateShort(map.get("wx_account")==null?"":map.get("wx_account").toString(), template_id_short, null,data,min);
                pushTemplateService.saveAndSendPushWxTemplate(templateShort);
            }
        }


    }

    @Override
    public Page<TbLibUserApply> applyBookHisList(Map<String, Object> params, Pageable pageable) {
        return queryDao.applyBookHisList(params,pageable);
    }
}
