package com.miaxis.coach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.coach.domain.CoachInfo;
import com.miaxis.coach.dto.CoachInfoDTO;
import com.miaxis.coach.dto.CoachInfoPcDTO;
import com.miaxis.coach.dto.CoachInfoVO;
import com.miaxis.coach.mapper.CoachInfoMapper;
import com.miaxis.coach.service.ICoachInfoService;
import com.miaxis.coach.vo.CoachInfoPcVO;
import com.miaxis.common.annotation.DataScope;
import com.miaxis.common.utils.DateUtils;
import com.miaxis.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 教练Service业务层处理
 *
 * @author wwl
 * @date 2021-01-07
 */
@Service
public class CoachInfoServiceImpl extends ServiceImpl<CoachInfoMapper, CoachInfo> implements ICoachInfoService {

    @Resource
    private CoachInfoMapper coachInfoMapper;

    /**
     * 查询教练
     *
     * @param coachnum 教练ID
     * @return 教练
     */
    @Override
    public CoachInfo selectCoachInfoById(String coachnum){
        return coachInfoMapper.selectCoachInfoById(coachnum);
    }

    /**
     * 查询教练列表
     *
     * @param coachInfo 教练
     * @return 教练
     */
    @Override
    public List<CoachInfo> selectCoachInfoList(CoachInfo coachInfo){
        return coachInfoMapper.selectCoachInfoList(coachInfo);
    }

    /**
     * 新增教练
     *
     * @param coachInfo 教练
     * @return 结果
     */
    @Override
    public int insertCoachInfo(CoachInfo coachInfo){
        coachInfo.setCreateTime(DateUtils.getNowDate());
        return coachInfoMapper.insertCoachInfo(coachInfo);
    }

    /**
     * 修改教练
     *
     * @param coachInfo 教练
     * @return 结果
     */
    @Override
    public int updateCoachInfo(CoachInfo coachInfo){
        coachInfo.setUpdateTime(DateUtils.getNowDate());
        return coachInfoMapper.updateCoachInfo(coachInfo);
    }

    /**
     * 批量删除教练
     *
     * @param coachnums 需要删除的教练ID
     * @return 结果
     */
    @Override
    public int deleteCoachInfoByIds(String[] coachnums){
        return coachInfoMapper.deleteCoachInfoByIds(coachnums);
    }


    @Override
    public List<CoachInfoVO> selectCoachInfoUrlList(CoachInfoDTO coachInfoDTO) {
        return coachInfoMapper.selectCoachInfoUrlList(coachInfoDTO);
    }

    /**
     * pc查询教练列表
     */
    @Override
    @DataScope(deptAlias ="d")
    public List<CoachInfoPcVO> queryList(CoachInfoPcDTO coachInfo) {
        return coachInfoMapper.queryList(coachInfo);
    }

    /**
     * pc教练密码重置
     * @param coachnums
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean resetPassword(String[] coachnums) {
        try {
            String password = SecurityUtils.encryptPassword("123456");
            coachInfoMapper.resetPassword(coachnums, password);
            return true;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

}
