package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmSysRolePowerDao;
import com.ssm.manage.dao.SsmSysStoreRoleDao;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.entity.SysRole;
import com.ssm.manage.entity.SysRolePower;
import com.ssm.manage.entity.SysStoreRole;
import com.ssm.manage.mapper.SsmSysStoreRoleMapper;
import com.ssm.manage.mapper.SysRoleUserMapper;
import com.ssm.manage.query.RoleRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.query.SysStoreRoleRequest;
import com.ssm.manage.service.SsmBaseStoreService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component
@Slf4j
public class SsmSysStoreRoleDaoImpl  extends BaseDaoImpl<SysStoreRole> implements SsmSysStoreRoleDao {
    @Autowired
    private SsmSysStoreRoleMapper mapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Override
    public SsmSysStoreRoleMapper getMapper() {
        return mapper;
    }

    @Override
        public PageInfo<SysStoreRole> selectPage(SysStoreRoleRequest sysStoreRole) {

        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(sysStoreRole.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                sysStoreRole.setStoreId(pagesorganization.getId());
            }
        }

        Example example = new Example(SysStoreRole.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysStoreRole.getRoleNum())){
            criteria.andLike("roleNum","%" + sysStoreRole.getRoleNum() + "%");
        }
        if (StringUtils.isNotBlank(sysStoreRole.getRoleName())){
            criteria.andLike("roleName","%" + sysStoreRole.getRoleName() + "%");
        }
        if (null != sysStoreRole.getState()){
            criteria.andEqualTo("state",sysStoreRole.getState());
        }
//        criteria.andNotEqualTo("roleNum","root");
        criteria.andEqualTo("storeId",sysStoreRole.getStoreId());
//        PageHelper.startPage(roleRequest.getPageNum(),roleRequest.getPageSize(),"role_id desc");
        example.orderBy("founderDatetime");
        List<SysStoreRole> list =  selectByExample(example);
        PageInfo<SysStoreRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
