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

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.organisation.SysCliqueOrg;
import cn.superdesk.uniorder.dao.entity.organisation.SysCliqueOrgExample;
import cn.superdesk.uniorder.dao.mapper.organisation.SysCliqueOrgMapper;
import cn.superdesk.uniorder.dao.mapper.organisation.SysCliqueOrgMapperEx;
import cn.superdesk.uniorder.service.organisation.SysCliqueOrgService;
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
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-04-28 14:08:25
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-04-28 14:08:25
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SysCliqueOrgServiceImpl implements SysCliqueOrgService {

    @Autowired
    private SysCliqueOrgMapper   sysCliqueOrgMapper;
    @Autowired
    private SysCliqueOrgMapperEx sysCliqueOrgMapperEx;


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

    @Override
    public int insertSelective(SysCliqueOrg entity) {
        return sysCliqueOrgMapper.insert(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysCliqueOrg entity) {
        return sysCliqueOrgMapper.updateByPrimaryKeySelective(entity);

    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysCliqueOrg findById(Long id) {
//		return sysCliqueOrgMapper.selectByPrimaryKey(id);
//		return null;

        SysCliqueOrgExample example = new SysCliqueOrgExample();
        example.createCriteria()
                .andIdEqualTo(id);

        List<SysCliqueOrg> resultList = sysCliqueOrgMapper.selectByExample(example);

        if (AnCollectionUtils.isNotEmpty(resultList)) {
            return resultList.get(0);
        } else {
            return null;
        }

    }

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

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

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


    public void addSysCliqueOrg(SysCliqueOrg entity) {
        sysCliqueOrgMapper.insertSelective(entity);
    }

    public void updateSysCliqueOrg(SysCliqueOrg entity) {
//		SysCliqueOrg originEntity = sysCliqueOrgMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
//		AnBeanUtils.copy(entity, originEntity);
//		sysCliqueOrgMapper.updateByPrimaryKey(originEntity);

    }

    public void deleteSysCliqueOrg(Long id) {
//		sysCliqueOrgMapper.deleteByPrimaryKey(id);
    }

    public SysCliqueOrg findSysCliqueOrgById(Long id) {
//		SysCliqueOrg entity = sysCliqueOrgMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
//		return entity;

        return null;
    }

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

        // 样例 保留
//		SysCliqueOrg sysCliqueOrgConditions = BeanUtils.mapToBean(conditions,SysCliqueOrg.class);

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

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

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

    }

    /****************************************** kafka start ******************************************/

    public void kafkaMergeReceiveCliqueOrg(SysCliqueOrg sysCliqueOrgEntity) {

        try {


            SysCliqueOrg sysCliqueOrgIsExist = findById(sysCliqueOrgEntity.getId());

            if (null == sysCliqueOrgIsExist) {
                sysCliqueOrgMapperEx.insertSelective(sysCliqueOrgEntity);
            } else {

                SysCliqueOrgExample example = new SysCliqueOrgExample();
                example.createCriteria()
                        .andIdEqualTo(sysCliqueOrgEntity.getId());
//						.andCliqueIdEqualTo(sysCliqueOrgEntity.getCliqueId())
//						.andOrgIdEqualTo(sysCliqueOrgEntity.getOrgId())
//						.andHigherPidEqualTo(sysCliqueOrgEntity.getHigherPid());
                sysCliqueOrgMapper.updateByExampleSelective(sysCliqueOrgEntity, example);
            }

        } catch (Exception e) {

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

        }
    }

    public void kafkaDeleteReceiveCliqueOrg(SysCliqueOrg sysCliqueOrgEntity) {

        SysCliqueOrgExample example = new SysCliqueOrgExample();
        example.createCriteria()
                .andIdEqualTo(sysCliqueOrgEntity.getId())
                .andCliqueIdEqualTo(sysCliqueOrgEntity.getCliqueId())
                .andOrgIdEqualTo(sysCliqueOrgEntity.getOrgId())
                .andHigherPidEqualTo(sysCliqueOrgEntity.getHigherPid());

        List<SysCliqueOrg> checkExists = sysCliqueOrgMapper.selectByExample(example);

        if (AnCollectionUtils.isNotEmpty(checkExists)) {
            for (SysCliqueOrg item : checkExists) {
//				item.setEnabled(CommonEnabledEnum.禁用.getKey());
//				item.setDeleted(CommonSoftDeleteEnum.删除.getKey());
//				sysCliqueOrgMapper.updateByPrimaryKey(item);

                sysCliqueOrgMapper.deleteByExample(example);
            }
        }
    }

    /****************************************** kafka   end ******************************************/

}