package com.ruoyi.mingxue.service.impl;

import com.ruoyi.mingxue.domain.CustomerVideoWatches;
import com.ruoyi.mingxue.mapper.CustomerVideoWatchesMapper;
import com.ruoyi.mingxue.service.ICustomerVideoWatchesService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 客户观看视频记录Service业务层处理
 *
 * @author mingxue
 * @date 2021-07-28
 */
@Service
public class CustomerVideoWatchesServiceImpl implements ICustomerVideoWatchesService
{
    @Autowired
    private CustomerVideoWatchesMapper customerVideoWatchesMapper;

    /**
     * 查询客户观看视频记录
     *
     * @param id 客户观看视频记录ID
     * @return 客户观看视频记录
     */
    @Override
    public CustomerVideoWatches selectCustomerVideoWatchesById(Long id)
    {
        return customerVideoWatchesMapper.selectCustomerVideoWatchesById(id);
    }

    /**
     * 查询客户观看视频记录列表
     *
     * @param customerVideoWatches 客户观看视频记录
     * @return 客户观看视频记录
     */
    @Override
    public List<CustomerVideoWatches> selectCustomerVideoWatchesList(CustomerVideoWatches customerVideoWatches)
    {
        return customerVideoWatchesMapper.selectCustomerVideoWatchesList(customerVideoWatches);
    }

    /**
     * 新增客户观看视频记录
     *
     * @param customerVideoWatches 客户观看视频记录
     * @return 结果
     */
    @Override
    public int insertCustomerVideoWatches(CustomerVideoWatches customerVideoWatches)
    {
        return customerVideoWatchesMapper.insertCustomerVideoWatches(customerVideoWatches);
    }

    /**
     * 修改客户观看视频记录
     *
     * @param customerVideoWatches 客户观看视频记录
     * @return 结果
     */
    @Override
    public int updateCustomerVideoWatches(CustomerVideoWatches customerVideoWatches)
    {
        return customerVideoWatchesMapper.updateCustomerVideoWatches(customerVideoWatches);
    }

    /**
     * 批量删除客户观看视频记录
     *
     * @param ids 需要删除的客户观看视频记录ID
     * @return 结果
     */
    @Override
    public int deleteCustomerVideoWatchesByIds(Long[] ids)
    {
        return customerVideoWatchesMapper.deleteCustomerVideoWatchesByIds(ids);
    }

    /**
     * 删除客户观看视频记录信息
     *
     * @param id 客户观看视频记录ID
     * @return 结果
     */
    @Override
    public int deleteCustomerVideoWatchesById(Long id)
    {
        return customerVideoWatchesMapper.deleteCustomerVideoWatchesById(id);
    }

    @Override
    public int countVideoWatchCount(String customerCode) {
        return customerVideoWatchesMapper.countVideoWatchCount(customerCode);
    }

    @Override
    public int countVideoWatchDuration(String customerCode) {
        return customerVideoWatchesMapper.countVideoWatchDuration(customerCode);
    }

    @Override
    public List<CustomerVideoWatches> countDurationByDate(String customerCode, Date start, Date end) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("customerCode", customerCode);
        map.put("start", dateFormat.format(start));
        map.put("end", dateFormat.format(end));
        return customerVideoWatchesMapper.countDurationByDate(map);
    }

    @Override
    public List<CustomerVideoWatches> selectByDate(String customerCode, Date start, Date end) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("customerCode", customerCode);
        map.put("start", dateFormat.format(start));
        map.put("end", dateFormat.format(end));
        return customerVideoWatchesMapper.selectByDate(map);
    }

    @Override
    public List<CustomerVideoWatches> countDurationByDateAndSubject(String customerCode, Date start, Date end) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("customerCode", customerCode);
        map.put("start", dateFormat.format(start));
        map.put("end", dateFormat.format(end));
        return customerVideoWatchesMapper.countDurationByDateAndSubject(map);
    }

    @Override
    public CustomerVideoWatches selectTodayBySection(String customerCode, Integer type, String sectionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("customerCode", customerCode);
        params.put("type", type);
        params.put("sectionId", sectionId);
        List<CustomerVideoWatches> list = customerVideoWatchesMapper.selectTodayBySection(params);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
}
