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

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.UnifiedStatisticsVirt;
import cn.superdesk.uniorder.dao.entity.marketing.UnifiedStatisticsVirtExample;
import cn.superdesk.uniorder.dao.mapper.marketing.UnifiedStatisticsVirtDimMonthMapperEx;
import cn.superdesk.uniorder.dao.mapper.marketing.UnifiedStatisticsVirtMapper;
import cn.superdesk.uniorder.dao.mapper.marketing.UnifiedStatisticsVirtMapperCommon;
import cn.superdesk.uniorder.dto.param.marketing.UnifiedStatisticsVirtQueryCriteria;
import cn.superdesk.uniorder.service.marketing.UnifiedStatisticsVirtDimMonthService;
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 UnifiedStatisticsVirtDimMonthServiceImpl implements UnifiedStatisticsVirtDimMonthService {

    @Autowired
    private UnifiedStatisticsVirtMapper           unifiedStatisticsVirtMapper;
    @Autowired
    private UnifiedStatisticsVirtDimMonthMapperEx unifiedStatisticsVirtDimMonthMapperEx;

    @Autowired
    private UnifiedStatisticsVirtMapperCommon unifiedStatisticsVirtMapperCommon;


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


    @Override
    public int insertSelective(UnifiedStatisticsVirt entity) {
        return unifiedStatisticsVirtMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedStatisticsVirt entity) {
        return unifiedStatisticsVirtMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedStatisticsVirt findById(Long id) {
        return unifiedStatisticsVirtMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addUnifiedStatisticsVirt(UnifiedStatisticsVirt entity) {
        unifiedStatisticsVirtMapper.insertSelective(entity);
    }

    public void updateUnifiedStatisticsVirt(UnifiedStatisticsVirt entity) {
        UnifiedStatisticsVirt originEntity = unifiedStatisticsVirtMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedStatisticsVirtMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedStatisticsVirt(Long id) {
        unifiedStatisticsVirtMapper.deleteByPrimaryKey(id);
    }

    public UnifiedStatisticsVirt findUnifiedStatisticsVirtById(Long id) {
        UnifiedStatisticsVirt entity = unifiedStatisticsVirtMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

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

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

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

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

    }

    public R<List<UnifiedStatisticsVirt>> pageQueryGenericParam(UnifiedStatisticsVirtQueryCriteria queryCriteria) {

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

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

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

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

    }

    /****************************************** 统计 start ******************************************/
    /**
     * 周期统计企业订单 TODO 重构 mark by linjinyu at 20210906
     *
     * @param item
     * @param year
     * @param month
     */
    @Override
    public void storeVirtOrderStatisticalDimMonth(/* 重新通过sql统计出来的 */UnifiedStatisticsVirt item, Integer year, Integer month) {


        UnifiedStatisticsVirt searchModel = new UnifiedStatisticsVirt();

        searchModel.setServiceNo(item.getServiceNo());
        searchModel.setVirtId(item.getVirtId());
        searchModel.setSYear(item.getSYear());
        searchModel.setSMonth(item.getSMonth());

        UnifiedStatisticsVirt isExist = unifiedStatisticsVirtMapperCommon.selectByModel(searchModel);

        if (isExist != null) {
            item.setId(isExist.getId());
            item.setUpdateTime(DateUtil.date());
            unifiedStatisticsVirtMapper.updateByPrimaryKeySelective(item);
        } else {

            item.setCreateTime(DateUtil.date());

            unifiedStatisticsVirtMapper.insertSelective(item);
        }
    }

    @Override
    public List<UnifiedStatisticsVirt> queryVirtOrderStatisticalDimMonth(Map<String, Object> queryMap) {

        List<UnifiedStatisticsVirt> resultList = unifiedStatisticsVirtDimMonthMapperEx.queryVirtOrderStatistical(queryMap);
        return resultList;
    }


}