package com.sv.service.oms;

import com.github.pagehelper.PageHelper;
import com.sv.entity.Coach;
import com.sv.entity.Platform;
import com.sv.exception.oms.OmsException;
import com.sv.mapper.CoachMapper;
import com.sv.service.common.OSSClientUtil;
import com.sv.service.common.PlatformService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.service.CacheService;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import com.ydd.framework.core.service.impl.WxXCXService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * Service - 教练
 *
 * @author zhaoziyu
 * @since 2018-08-06
 */
@Service
@Transactional(readOnly = true)
public class CoachService extends BaseServiceImpl {

    private final Logger logger = LoggerFactory.getLogger(CoachService.class);

    @Resource
    private CoachMapper coachMapper;

    @Resource(name = "VenueLessonServiceOms")
    private VenueLessonService venueLessonService;

    @Resource
    private PlatformService platformService;

    @Resource
    private OSSClientUtil ossClientUtil;

    @Resource
    private CacheService cacheService;

    /**
     * 创建教练
     *
     * @param coach 教练
     */

    @Transactional
    public void save(Coach coach) {
        coach.setPlatformId(1);
        if (coach.getId() != null && coach.getId() > 0) {
            // 更新
            coachMapper.update(coach);
        } else {
            // 新建

            Platform platform = platformService.findById(1);
            coachMapper.insert(coach);
            InputStream inputStream = WxXCXService.getNewInstance(platform.getAppId(), platform.getSecret(), "wx_xcx_token", cacheService).getXCXQrCode("pages/myBookings/index", coach.getId().toString(), "190");
            String url = ossClientUtil.uploadImg(inputStream, "qrcode.png");
            coach.setQrCode(url);
            coachMapper.update(coach);
        }
    }

    /**
     * 更新教练
     *
     * @param coach 教练
     */

    @Transactional
    public void update(Coach coach) {
        coachMapper.update(coach);
    }



    /**
     * 删除教练
     *
     * @param id 编号
     * @return 删除数量
     */

    @Transactional
    public Integer delete(Integer id) {
        if (id == null || id <= 0) {
            return 0;
        }
        return coachMapper.delete(id);
    }

    /**
     * 删除教练
     *
     * @param ids 编号数组
     * @return 删除数量
     */

    @Transactional
    public Integer deleteByIds(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        for (Integer id : ids) {
            if (venueLessonService.countByCoachId(id) > 0) {
                throw new OmsException("该教练有课程，无法删除！");
            }
        }
        return coachMapper.deleteByIds(ids);
    }

    /**
     * 查询教练
     *
     * @param id 编号
     * @return 教练
     */
    public Coach findById(Integer id) {
        return coachMapper.findById(id);
    }

    /**
     * 查询教练
     *
     * @param pagination 分页信息
     * @return 分页结果
     */

    public Pagination findPage(Pagination pagination) {
        PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
        PageHelper.orderBy("id desc");
        pagination.setQueryResult(coachMapper.findAll());
        return pagination;
    }


    /**
     * 通过场馆类型搜索场馆
     *
     * @param venueType
     * @return
     */
    public List<Coach> findByVenueType(Integer venueType) {
        return coachMapper.findByVenueType(venueType);
    }
}

