package com.gxwebsoft.hjm.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.impl.UserServiceImpl;
import com.gxwebsoft.hjm.dto.TemplateMessageRequest;
import com.gxwebsoft.hjm.entity.HjmCar;
import com.gxwebsoft.hjm.entity.HjmViolation;
import com.gxwebsoft.hjm.mapper.HjmViolationMapper;
import com.gxwebsoft.hjm.param.HjmViolationParam;
import com.gxwebsoft.hjm.service.HjmCarService;
import com.gxwebsoft.hjm.service.HjmViolationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 黄家明_违章记录Service实现
 *
 * @author 科技小王子
 * @since 2025-06-20 13:48:43
 */
@Service
public class HjmViolationServiceImpl extends ServiceImpl<HjmViolationMapper, HjmViolation> implements HjmViolationService {

  @Resource
  private HjmCarService hjmCarService;
  @Resource
  private WxNotificationServiceImpl wxNotificationService;
  @Resource
  private UserServiceImpl userService;

  @Override
  public PageResult<HjmViolation> pageRel(HjmViolationParam param) {
    PageParam<HjmViolation, HjmViolationParam> page = new PageParam<>(param);
    page.setDefaultOrder("sort_number asc, create_time desc");
    List<HjmViolation> list = baseMapper.selectPageRel(page, param);
    return new PageResult<>(list, page.getTotal());
  }

  @Override
  public List<HjmViolation> listRel(HjmViolationParam param) {
    List<HjmViolation> list = baseMapper.selectListRel(param);
    // 排序
    PageParam<HjmViolation, HjmViolationParam> page = new PageParam<>();
    page.setDefaultOrder("sort_number asc, create_time desc");
    return page.sortRecords(list);
  }

  @Override
  public HjmViolation getByIdRel(Integer id) {
    HjmViolationParam param = new HjmViolationParam();
    param.setId(id);
    return param.getOne(baseMapper.selectListRel(param));
  }

  @Override
  public void send(HjmViolation hjmViolation) {
    final HjmCar item = hjmCarService.getByCode(hjmViolation.getCode());

    // 获取所有的邮政协会/管局工作人员
    final List<User> users = userService.listByAlert();
    if (ObjUtil.isEmpty(item)) {
      return;
    }
    users.forEach(d -> {
      item.setToUser(d.getOfficeOpenid());
      item.setAppId("wxd2723d1afd9c4553");
      sendTemplateMessage(item, hjmViolation);
    });

  }

  public void sendTemplateMessage(HjmCar item, HjmViolation violation) {
    // 发送模板消息
    final TemplateMessageRequest templateMessageRequest = new TemplateMessageRequest();
    templateMessageRequest.setToUser(item.getToUser());
    templateMessageRequest.setTemplateId("gZshS5yJs47BhIFodo9yenZcmsVwJOCKkL-SYaZTioU");
    final TemplateMessageRequest.MiniProgram miniProgram = new TemplateMessageRequest.MiniProgram();
    miniProgram.setAppid(item.getAppId());
    miniProgram.setPagepath("hjm/violation/detail?id=".concat(item.getCode()));
//    miniProgram.setPagepath("hjm/query?id=".concat(item.getCode()));
    templateMessageRequest.setMiniprogram(miniProgram);
    HashMap<String, TemplateMessageRequest.TemplateDataItem> map = new HashMap<>();
    map.put("thing7", new TemplateMessageRequest.TemplateDataItem(item.getDriverName()));
    map.put("phone_number8", new TemplateMessageRequest.TemplateDataItem(item.getDriverPhone()));
    map.put("const4", new TemplateMessageRequest.TemplateDataItem("违章"));
    map.put("car_number1", new TemplateMessageRequest.TemplateDataItem(item.getCode()));
    // 获取当前时间,格式2024年1月1号 10:20
    map.put("time2", new TemplateMessageRequest.TemplateDataItem(
      new SimpleDateFormat("yyyy年M月d日 HH:mm").format(LocalDateTime.now()), "#173177")
    );
    System.out.println("map = " + map);
    templateMessageRequest.setData(map);
    boolean success = wxNotificationService.sendTemplateMessage(10519, templateMessageRequest);
    System.out.println("2向 = " + item.getDriverName() + "发送消息成功：" + success);
  }

}
