package cn.superdesk.uniorder.service.marketing.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.marketing.UnifiedStatisticsVirtDimDay;
import cn.superdesk.uniorder.dao.entity.marketing.UnifiedStatisticsVirtDimDayExample;
import cn.superdesk.uniorder.dao.mapper.marketing.UnifiedStatisticsVirtDimDayMapper;
import cn.superdesk.uniorder.dao.mapper.marketing.UnifiedStatisticsVirtDimDayMapperEx;
import cn.superdesk.uniorder.dto.param.marketing.UnifiedStatisticsVirtDimDayQueryCriteria;
import cn.superdesk.uniorder.service.marketing.UnifiedStatisticsVirtDimDayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-09-06 12:00:09
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-09-06 12:00:09
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UnifiedStatisticsVirtDimDayServiceImpl implements UnifiedStatisticsVirtDimDayService {

    @Autowired
    private UnifiedStatisticsVirtDimDayMapper   unifiedStatisticsVirtDimDayMapper;
    @Autowired
    private UnifiedStatisticsVirtDimDayMapperEx unifiedStatisticsVirtDimDayMapperEx;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(UnifiedStatisticsVirtDimDay entity) {
        return unifiedStatisticsVirtDimDayMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedStatisticsVirtDimDay entity) {
        return unifiedStatisticsVirtDimDayMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedStatisticsVirtDimDay findById(Long id) {
        return unifiedStatisticsVirtDimDayMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<UnifiedStatisticsVirtDimDay> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<UnifiedStatisticsVirtDimDay> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addUnifiedStatisticsVirtDimDay(UnifiedStatisticsVirtDimDay entity) {
        unifiedStatisticsVirtDimDayMapper.insertSelective(entity);
    }

    public void updateUnifiedStatisticsVirtDimDay(UnifiedStatisticsVirtDimDay entity) {
        UnifiedStatisticsVirtDimDay originEntity = unifiedStatisticsVirtDimDayMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedStatisticsVirtDimDayMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedStatisticsVirtDimDay(Long id) {
        unifiedStatisticsVirtDimDayMapper.deleteByPrimaryKey(id);
    }

    public UnifiedStatisticsVirtDimDay findUnifiedStatisticsVirtDimDayById(Long id) {
        UnifiedStatisticsVirtDimDay entity = unifiedStatisticsVirtDimDayMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public R<List<UnifiedStatisticsVirtDimDay>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        UnifiedStatisticsVirtDimDayExample          example  = new UnifiedStatisticsVirtDimDayExample();
        UnifiedStatisticsVirtDimDayExample.Criteria criteria = example.createCriteria();

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 UnifiedStatisticsVirtDimDayExample.Criteria
        // UnifiedStatisticsVirtDimDay unifiedStatisticsVirtDimDayConditions = BeanUtils.mapToBean(conditions,UnifiedStatisticsVirtDimDay.class);

        PageHelper.startPage(pageNun, pageSize);
        List<UnifiedStatisticsVirtDimDay>     resultList = unifiedStatisticsVirtDimDayMapper.selectByExample(example);
        PageInfo<UnifiedStatisticsVirtDimDay> page       = new PageInfo<UnifiedStatisticsVirtDimDay>(resultList);
        PageHelper.clearPage();

        return new R<List<UnifiedStatisticsVirtDimDay>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<UnifiedStatisticsVirtDimDay>> pageQueryGenericParam(UnifiedStatisticsVirtDimDayQueryCriteria queryCriteria) {

        UnifiedStatisticsVirtDimDayExample          example  = new UnifiedStatisticsVirtDimDayExample();
        UnifiedStatisticsVirtDimDayExample.Criteria criteria = example.createCriteria();

        // TODO 添加条件... 后期添加 queryCriteria 转 UnifiedStatisticsVirtDimDayExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<UnifiedStatisticsVirtDimDay>     resultList = unifiedStatisticsVirtDimDayMapper.selectByExample(example);
        PageInfo<UnifiedStatisticsVirtDimDay> page       = new PageInfo<UnifiedStatisticsVirtDimDay>(resultList);
        PageHelper.clearPage();

        return new R<List<UnifiedStatisticsVirtDimDay>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    /****************************************** 统计 start ******************************************/

    public void storeVirtOrderStatisticalDimDay(UnifiedStatisticsVirtDimDay item, Integer year, Integer month, Integer dayOfMonth) {

        UnifiedStatisticsVirtDimDayExample          example  = new UnifiedStatisticsVirtDimDayExample();
        UnifiedStatisticsVirtDimDayExample.Criteria criteria = example.createCriteria();

        criteria
                .andServiceNoEqualTo(item.getServiceNo())
                .andVirtIdEqualTo(item.getVirtId())
                .andSYearEqualTo(item.getSYear())
                .andSMonthEqualTo(item.getSMonth())
                .andSDayEqualTo(item.getSDay());

        List<UnifiedStatisticsVirtDimDay> resultList = unifiedStatisticsVirtDimDayMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(resultList)) {

            UnifiedStatisticsVirtDimDay isExist = resultList.get(0);
            item.setId(isExist.getId());
            item.setUpdateTime(DateUtil.date());

            unifiedStatisticsVirtDimDayMapper.updateByPrimaryKeySelective(item);

        } else {
            item.setCreateTime(DateUtil.date());
            unifiedStatisticsVirtDimDayMapper.insertSelective(item);
        }

    }

    public List<UnifiedStatisticsVirtDimDay> queryVirtOrderStatisticalDimDay(Map<String, Object> queryMap) {

        List<UnifiedStatisticsVirtDimDay> resultList = unifiedStatisticsVirtDimDayMapperEx.queryVirtOrderStatistical(queryMap);
        return resultList;

    }

}