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

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.exception.utils.AssertUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelProfitSync;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelProfitSyncExample;
import cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapper;
import cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapperEx;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskapp.UnifiedChannelProfitSyncReceiveParam;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedChannelProfitSyncPage;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.channel.UnifiedChannelProfitSyncService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 8/28/20 4:55 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 8/28/20 4:55 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class UnifiedChannelProfitSyncServiceImpl implements UnifiedChannelProfitSyncService {

    @Autowired
    private UnifiedChannelProfitSyncMapper   unifiedChannelProfitSyncMapper;
    @Autowired
    private UnifiedChannelProfitSyncMapperEx unifiedChannelProfitSyncMapperEx;
    @Autowired
    private CommonEventPublisher             commonEventPublisher;


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

    @Override
    public int insertSelective(UnifiedChannelProfitSync entity) {
        return unifiedChannelProfitSyncMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedChannelProfitSync entity) {
        return unifiedChannelProfitSyncMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
        unifiedChannelProfitSyncMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UnifiedChannelProfitSync findById(Long id) {
        return unifiedChannelProfitSyncMapper.selectByPrimaryKey(id);
    }

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

    @Override
    public List<UnifiedChannelProfitSync> findAll(Map<String, Object> queryMap) {
        return unifiedChannelProfitSyncMapperEx.findAll(queryMap);
    }

    ///////////////////////


    @Override
    public UnifiedChannelProfitSync findChannelProfitBySyncId(Long id) {
        return unifiedChannelProfitSyncMapperEx.findChannelProfitBySyncId(id);
    }

    @Override
    public R<List<UnifiedChannelProfitSyncPage>> getChannelPage(Map<String, Object> params, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<UnifiedChannelProfitSyncPage> ls = unifiedChannelProfitSyncMapperEx.getPageData(params);

        PageInfo<UnifiedChannelProfitSyncPage> page = new PageInfo<UnifiedChannelProfitSyncPage>(ls);
        PageHelper.clearPage();
        return new R<List<UnifiedChannelProfitSyncPage>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);
    }

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


    public void addUnifiedChannelProfitSync(UnifiedChannelProfitSync entity) {
        unifiedChannelProfitSyncMapper.insertSelective(entity);
    }

    public void updateUnifiedChannelProfitSync(UnifiedChannelProfitSync entity) {
        UnifiedChannelProfitSync originEntity = unifiedChannelProfitSyncMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedChannelProfitSyncMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedChannelProfitSync(Long id) {
        unifiedChannelProfitSyncMapper.deleteByPrimaryKey(id);
    }

    public UnifiedChannelProfitSync findUnifiedChannelProfitSyncById(Long id) {
        UnifiedChannelProfitSync entity = unifiedChannelProfitSyncMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		UnifiedChannelProfitSync unifiedChannelProfitSyncConditions = AnBeanUtils.mapToBean(conditions,UnifiedChannelProfitSync.class);

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

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

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

    }


    public void deleteReceiveUnifiedChannelProfitSync(String message) {

        // string 转换 param
        UnifiedChannelProfitSyncReceiveParam unifiedChannelProfitSyncReceiveParam = AnJsonUtils.toObject(message, UnifiedChannelProfitSyncReceiveParam.class);

        UnifiedChannelProfitSync unifiedChannelProfitSync = unifiedChannelProfitSyncMapperEx.findChannelProfitBySyncId(unifiedChannelProfitSyncReceiveParam.getId());

        // 存在
        if (unifiedChannelProfitSync != null) {
            unifiedChannelProfitSyncMapper.deleteByPrimaryKey(unifiedChannelProfitSync.getId());

            // TODO 创建快速 查找 分润比较的码表
            // UnifiedChannelProfitXVirt
            commonEventPublisher.triggerChannelProfitSyncCacheMappingEvent("delete", unifiedChannelProfitSync);

        }


    }

    public void mergeReceiveUnifiedChannelProfitSync(String message) {

        try {

            // string 转换 param
            UnifiedChannelProfitSyncReceiveParam unifiedChannelProfitSyncReceiveParam = AnJsonUtils.toObject(message, UnifiedChannelProfitSyncReceiveParam.class);

            log.info(AnJsonUtils.toJson(unifiedChannelProfitSyncReceiveParam));

            // 查找 存在不?
            UnifiedChannelProfitSync unifiedChannelProfitSync = unifiedChannelProfitSyncMapperEx.findChannelProfitBySyncId(unifiedChannelProfitSyncReceiveParam.getId());

            if (null == unifiedChannelProfitSync) {

                unifiedChannelProfitSync = new UnifiedChannelProfitSync();

                convertReceiveUnifiedChannelProfitSyncParam2Entity(unifiedChannelProfitSyncReceiveParam, unifiedChannelProfitSync);

                unifiedChannelProfitSyncMapper.insertSelective(unifiedChannelProfitSync);

                // TODO 创建快速 查找 分润比较的码表
                // UnifiedChannelProfitXVirt
                commonEventPublisher.triggerChannelProfitSyncCacheMappingEvent("insert", unifiedChannelProfitSync);

                // TODO 维护 数据权限许可
                commonEventPublisher.triggerChannelProfitSyncCreateSysRoleExtDataSampleEvent("insert", unifiedChannelProfitSync);

            } else {

                convertReceiveUnifiedChannelProfitSyncParam2Entity(unifiedChannelProfitSyncReceiveParam, unifiedChannelProfitSync);

                unifiedChannelProfitSyncMapper.updateByPrimaryKey(unifiedChannelProfitSync);

                // TODO 创建快速 查找 分润比较的码表
                // UnifiedChannelProfitXVirt
                commonEventPublisher.triggerChannelProfitSyncCacheMappingEvent("update", unifiedChannelProfitSync);

                // TODO 维护 数据权限许可
                commonEventPublisher.triggerChannelProfitSyncCreateSysRoleExtDataSampleEvent("update", unifiedChannelProfitSync);
            }


        } catch (Exception e) {
            log.error("服务之窗 同步 统一订单-mergeReceiveUnifiedChannelProfitSync 失败:\n{}", ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

    }

    /**
     * 转换
     *
     * @param unifiedChannelProfitSyncReceiveParam
     * @param unifiedChannelProfitSync
     */
    public void convertReceiveUnifiedChannelProfitSyncParam2Entity(UnifiedChannelProfitSyncReceiveParam unifiedChannelProfitSyncReceiveParam,
                                                                   UnifiedChannelProfitSync unifiedChannelProfitSync) {


        unifiedChannelProfitSync.setSyncId(unifiedChannelProfitSyncReceiveParam.getId());

        unifiedChannelProfitSync.setChannelId(unifiedChannelProfitSyncReceiveParam.getComapanyId());
        unifiedChannelProfitSync.setOrganizationId(unifiedChannelProfitSyncReceiveParam.getOrganizationId());

        unifiedChannelProfitSync.setBusinessCode(unifiedChannelProfitSyncReceiveParam.getBusinessCode());

        unifiedChannelProfitSync.setBeginDate(unifiedChannelProfitSyncReceiveParam.getBeginDate());
        unifiedChannelProfitSync.setEndDate(unifiedChannelProfitSyncReceiveParam.getEndDate());

        unifiedChannelProfitSync.setProportion(unifiedChannelProfitSyncReceiveParam.getProportion());

        // 坑位 姜勇 的问题 容错
        unifiedChannelProfitSync.setIsBindVirt(unifiedChannelProfitSyncReceiveParam.getIsBindVirt() == null ? 0 : unifiedChannelProfitSyncReceiveParam.getIsBindVirt());
        unifiedChannelProfitSync.setVirtIds(unifiedChannelProfitSyncReceiveParam.getVirtIds());

        unifiedChannelProfitSync.setSyncTime(DateUtil.date());

        unifiedChannelProfitSync.setCreateTime(unifiedChannelProfitSyncReceiveParam.getCreatedAt());
        unifiedChannelProfitSync.setCreateUserId(1L);

        unifiedChannelProfitSync.setUpdateTime(DateUtil.date());
        unifiedChannelProfitSync.setUpdateUserId(1L);

    }


}
