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.UnifiedChannelSync;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelSyncExample;
import cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelSyncMapper;
import cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelSyncMapperEx;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskapp.UnifiedChannelSyncReceiveParam;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.channel.UnifiedChannelSyncService;
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: 运营商 渠道商
 * @Author: linjinyu
 * @CreateDate: 9/1/20 4:42 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 9/1/20 4:42 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class UnifiedChannelSyncServiceImpl implements UnifiedChannelSyncService {

    @Autowired
    private UnifiedChannelSyncMapper   unifiedChannelSyncMapper;
    @Autowired
    private UnifiedChannelSyncMapperEx unifiedChannelSyncMapperEx;
    @Autowired
    private CommonEventPublisher       commonEventPublisher;


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


    @Override
    public int insertSelective(UnifiedChannelSync entity) {
        return unifiedChannelSyncMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedChannelSync entity) {
        return unifiedChannelSyncMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UnifiedChannelSync findById(Long id) {
        return unifiedChannelSyncMapper.selectByPrimaryKey(id);
    }

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

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


    ///////////


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


    public void addUnifiedChannelSync(UnifiedChannelSync entity) {
        unifiedChannelSyncMapper.insertSelective(entity);
    }

    public void updateUnifiedChannelSync(UnifiedChannelSync entity) {
        UnifiedChannelSync originEntity = unifiedChannelSyncMapper.selectByPrimaryKey(entity.getId());
        AssertUtils.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        unifiedChannelSyncMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUnifiedChannelSync(Long id) {
        unifiedChannelSyncMapper.deleteByPrimaryKey(id);
    }

    public UnifiedChannelSync findUnifiedChannelSyncById(Long id) {
        UnifiedChannelSync entity = unifiedChannelSyncMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		UnifiedChannelSync unifiedChannelSyncConditions = AnBeanUtils.mapToBean(conditions,UnifiedChannelSync.class);

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

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

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

    }


    @Override
    public UnifiedChannelSync findUnifiedChannelSyncByChannelId(Long id) {
        return unifiedChannelSyncMapperEx.findChannelByChannelId(id);
    }


    /***************************************************** 关于 UnifiedChannelSync kafka同步 start *****************************************************/
    public void deleteReceiveUnifiedChannelSync(String message) {


        // string 转换 param
        UnifiedChannelSyncReceiveParam unifiedChannelSyncReceiveParam = AnJsonUtils.toObject(message, UnifiedChannelSyncReceiveParam.class);


//		UnifiedChannelSync channelSyncs = unifiedChannelSyncDubboConsumerService.findUnifiedChannelSyncByChannelId(unifiedChannelSyncReceiveParam.getId());
//
//		// 存在
//		if (channelSyncs != null) {
//			unifiedChannelSyncDubboConsumerService.deleteUnifiedChannelSync(channelSyncs.getId());
//		}

        UnifiedChannelSync unifiedChannelSync = unifiedChannelSyncMapperEx.findChannelByChannelId(unifiedChannelSyncReceiveParam.getId());

        // 存在
        if (unifiedChannelSync != null) {
            unifiedChannelSyncMapper.deleteByPrimaryKey(unifiedChannelSync.getId());

            // TODO 维护 数据权限角色 但这个角色要挂到哪个用户 呢?  通过 leader_name 来查找 统一订单账号?
            commonEventPublisher.triggerChannelSyncCreateSysRoleExtDataEvent("delete", unifiedChannelSync);
        }


    }

    public void mergeReceiveUnifiedChannelSync(String message) {

        try {

            // string 转换 param
            UnifiedChannelSyncReceiveParam unifiedChannelSyncReceiveParam = AnJsonUtils.toObject(message, UnifiedChannelSyncReceiveParam.class);

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

            // 查找 存在不?
            UnifiedChannelSync unifiedChannelSync = unifiedChannelSyncMapperEx.findChannelByChannelId(unifiedChannelSyncReceiveParam.getId());

            if (null == unifiedChannelSync) {

                unifiedChannelSync = new UnifiedChannelSync();

                convertReceiveUnifiedChannelSyncParam2Entity(unifiedChannelSyncReceiveParam, unifiedChannelSync);
                unifiedChannelSync.setCreateTime(DateUtil.date());
                unifiedChannelSync.setCreateUserId(1L);
                unifiedChannelSyncMapper.insertSelective(unifiedChannelSync);

                // TODO 维护 数据权限角色
                commonEventPublisher.triggerChannelSyncCreateSysRoleExtDataEvent("insert", unifiedChannelSync);

            } else {

                convertReceiveUnifiedChannelSyncParam2Entity(unifiedChannelSyncReceiveParam, unifiedChannelSync);
                unifiedChannelSync.setUpdateTime(DateUtil.date());
                unifiedChannelSync.setUpdateUserId(1L);
                unifiedChannelSyncMapper.updateByPrimaryKey(unifiedChannelSync);

//				// TODO 维护 数据权限角色
                commonEventPublisher.triggerChannelSyncCreateSysRoleExtDataEvent("update", unifiedChannelSync);
            }


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

    }

    /**
     * 转换
     *
     * @param unifiedChannelSyncReceiveParam
     * @param unifiedChannelSync
     */
    public void convertReceiveUnifiedChannelSyncParam2Entity(UnifiedChannelSyncReceiveParam unifiedChannelSyncReceiveParam,
                                                             UnifiedChannelSync unifiedChannelSync) {


        unifiedChannelSync.setAddress(unifiedChannelSyncReceiveParam.getAddress());
        unifiedChannelSync.setChannelId(unifiedChannelSyncReceiveParam.getId());
        unifiedChannelSync.setChannelName(unifiedChannelSyncReceiveParam.getName());
        unifiedChannelSync.setCityCode(unifiedChannelSyncReceiveParam.getCityCode());
        unifiedChannelSync.setCityName(unifiedChannelSyncReceiveParam.getCityName());
        unifiedChannelSync.setCode(unifiedChannelSyncReceiveParam.getCode());

        unifiedChannelSync.setCompanyType(unifiedChannelSyncReceiveParam.getCompanyType());


        unifiedChannelSync.setEnabled(unifiedChannelSyncReceiveParam.getEnabled());

        unifiedChannelSync.setLeaderName(unifiedChannelSyncReceiveParam.getLeaderName());
        unifiedChannelSync.setLeaderPhone(unifiedChannelSyncReceiveParam.getLeaderPhone());

        // TODO add by linjinyu 20200526 start
        unifiedChannelSync.setLeaderType(unifiedChannelSyncReceiveParam.getLeaderType());
        unifiedChannelSync.setLeaderWxAccount(unifiedChannelSyncReceiveParam.getLeaderWxAccount());
        // TODO add by linjinyu 20200526 end

        unifiedChannelSync.setProvinceCode(unifiedChannelSyncReceiveParam.getProvinceCode());
        unifiedChannelSync.setProvinceName(unifiedChannelSyncReceiveParam.getProvinceName());
        unifiedChannelSync.setRemark(unifiedChannelSyncReceiveParam.getRemark());

        unifiedChannelSync.setSyncTime(DateUtil.date());


    }
    /***************************************************** 关于 UnifiedChannelSync kafka同步   end *****************************************************/
}
