package com.pj.project.tb_business_item;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.api.wx.bo.MsgDataBO;
import com.pj.api.wx.service.WxService;
import com.pj.current.config.MyConfig;
import com.pj.current.config.WxConfig;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project4sp.admin.SpAdmin;
import com.pj.project4sp.admin.SpAdminService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_business_item -- 业务作业项
 * @author qzy 
 */
@Service
@Transactional
public class TbBusinessItemService extends ServiceImpl<TbBusinessItemMapper,TbBusinessItem> implements IService<TbBusinessItem> {

	/** 底层 Mapper 对象 */
	@Autowired
	TbBusinessItemMapper tbBusinessItemMapper;
	@Resource
    TbBusinessService tbBusinessService;
    @Resource
    private SpAdminService spAdminService;
    @Resource
    private WxService wxService;
    @Resource
    private WxConfig wxConfig;
    @Resource
    private MyConfig myConfig;

	/** 增 */
	int add(TbBusinessItem t){
		return tbBusinessItemMapper.add(t);
	}



	/** 改 */
	int update(TbBusinessItem t){
		return tbBusinessItemMapper.update(t);
	}

	/** 查 */
	TbBusinessItem getById(String id){
		return tbBusinessItemMapper.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	public List<TbBusinessItem> getList(SoMap so) {
		return tbBusinessItemMapper.getList(so);	
	}


	public void removeByBusinessId(String businessId) {
		QueryWrapper<TbBusinessItem>ew=new QueryWrapper<>();
		ew.eq("business_id",businessId);
		this.remove(ew);
	}

    public List<TbBusinessItem> findByBusinessId(String businessId) {
		QueryWrapper<TbBusinessItem>ew=new QueryWrapper<>();
		ew.eq("business_id",businessId);
		ew.orderByDesc("pick");
		ew.orderByAsc("confirm");
		return list(ew);
    }

	public TbBusinessItem findByBusinessIdAndTypeName(String businessId, String itemTypeName) {
		QueryWrapper<TbBusinessItem>ew=new QueryWrapper<>();
		ew.eq("business_id",businessId);
		ew.eq("item_type_name",itemTypeName);
		return getOne(ew);
	}

    public void confirm(long id) {
        TbBusinessItem tbBusinessItem = this.getById(id);
        tbBusinessItem.setConfirm(1).setConfirmTime(new Date());
        this.updateById(tbBusinessItem);
        TbBusiness tbBusiness=tbBusinessService.getById(tbBusinessItem.getBusinessId());
        //todo 通知作业方
        List<SpAdmin> spAdminList = spAdminService.findByCustomerId(tbBusinessItem.getPickCustomerId());
        MsgDataBO msgDataBO=new MsgDataBO("您有一条新的业务确认提醒",tbBusiness.getCustomerName(), DateUtil.now(),tbBusinessItem.getItemTypeName()+"（"+tbBusinessItem.getItemName()+"）");
        spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
            String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + id;
            wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(),admin.getOpenid(),msgDataBO,detailUrl);
        });
    }
}
