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

import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
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.SysClique;
import cn.superdesk.uniorder.dao.entity.organisation.SysCliqueExample;
import cn.superdesk.uniorder.dao.mapper.organisation.SysCliqueMapper;
import cn.superdesk.uniorder.dao.mapper.organisation.SysCliqueMapperEx;
import cn.superdesk.uniorder.dto.param.organisation.SysCliqueFindByTreeCriteria;
import cn.superdesk.uniorder.dto.vo._function.tree.SysCliqueTreeModel;
import cn.superdesk.uniorder.service.organisation.SysCliqueService;
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 SysCliqueServiceImpl implements SysCliqueService {

    @Autowired
    private SysCliqueMapper   sysCliqueMapper;
    @Autowired
    private SysCliqueMapperEx sysCliqueMapperEx;


    /****************************************** extends BaseService 部分 start ******************************************/
    @Override
    public int insertSelective(SysClique entity) {
        return sysCliqueMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysClique entity) {
        return sysCliqueMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysClique findById(Long id) {
//		return sysCliqueMapper.selectByPrimaryKey(id);
        return null;
    }

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

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

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


    public void addSysClique(SysClique entity) {
        sysCliqueMapper.insertSelective(entity);
    }

    public void updateSysClique(SysClique entity) {
//		SysClique originEntity = sysCliqueMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
//		AnBeanUtils.copy(entity, originEntity);
//		sysCliqueMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteSysClique(Long id) {
//		sysCliqueMapper.deleteByPrimaryKey(id);
    }

    public SysClique findSysCliqueById(Long id) {
//		SysClique entity = sysCliqueMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
//		return entity;
        return null;
    }

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

        // 样例 保留
//		SysClique sysCliqueConditions = BeanUtils.mapToBean(conditions,SysClique.class);

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

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

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

    }

    /****************************************** kafka start ******************************************/
    public void kafkaMergeReceiveClique(SysClique sysCliqueEntity) {

        try {

            SysClique sysCliqueIsExist = findById(sysCliqueEntity.getId());

            if (null == sysCliqueIsExist) {

                sysCliqueMapperEx.insertSelective(sysCliqueEntity);

            } else {

                SysCliqueExample example = new SysCliqueExample();
                example.createCriteria()
                        .andIdEqualTo(sysCliqueEntity.getId());

                sysCliqueMapper.updateByExampleSelective(sysCliqueEntity, example);
            }

        } catch (Exception e) {

            e.printStackTrace();
            log.error("kafkaMergeReceiveClique 失败:{}", ExceptionUtils.getMessage(e));

        }
    }

    public void kafkaDeleteReceiveClique(SysClique sysCliqueEntity) {

        SysCliqueExample example = new SysCliqueExample();
        example.createCriteria()
                .andIdEqualTo(sysCliqueEntity.getId());


        List<SysClique> checkExists = sysCliqueMapper.selectByExample(example);

        if (AnCollectionUtils.isNotEmpty(checkExists)) {

            for (SysClique item : checkExists) {
                item.setEnabled(CommonEnabledEnum.禁用.getKey());
//				item.setDeleted(CommonSoftDeleteEnum.删除.getKey());
                sysCliqueMapper.updateByExampleSelective(item, example);
            }
        }

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


    /**
     * @return
     */
    public List<SysCliqueTreeModel> findByTree() {

//3.3.1.2 排序 TODO 再调整
//(1)集团按集团ID从小到大依次排列显示;
//(2)集团下分公司与服务之窗平台顺序一致;
//(3)集团下项目按项目ID从小到大依次排列显示;
//(4)项目下企业按企业ID从小到大依次排列显示;
//(5)搜索结果时,按ID从小到大依次排列显示,同ID随机排列显示;

        List<SysCliqueTreeModel> result = sysCliqueMapperEx.findByTree();

        return result;
    }

    public List<SysCliqueTreeModel> findByTree002(SysCliqueFindByTreeCriteria queryCriteria) {


        List<SysCliqueTreeModel> result = sysCliqueMapperEx.findByTree002(queryCriteria);

        return result;
    }

    public List<SysCliqueTreeModel> findByTree003(SysCliqueFindByTreeCriteria queryCriteria) {


        List<SysCliqueTreeModel> result = sysCliqueMapperEx.findByTree003(queryCriteria);

        return result;
    }

    public List<SysCliqueTreeModel> findByTree004(SysCliqueFindByTreeCriteria queryCriteria) {


        List<SysCliqueTreeModel> result = sysCliqueMapperEx.findByTree004(queryCriteria);

        return result;
    }


}
