fengxiang
2018-01-04 41e3ec869d1cda310cc3700cbe2682ef59627b95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.moral.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.moral.common.bean.PageBean;
import com.moral.common.util.ExampleUtil;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Organization;
import com.moral.entity.exp.OrganizationExp;
import com.moral.entity.OrganizationRelation;
import com.moral.mapper.OrganizationMapper;
import com.moral.mapper.OrganizationRelationMapper;
import com.moral.service.OrganizationService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
 
import javax.annotation.Resource;
import java.util.*;
 
import static com.moral.common.bean.Constants.IS_DELETE_FALSE;
 
@Service
public class OrganizationServiceImpl implements OrganizationService {
 
    @Resource
    private OrganizationMapper organizationMapper;
 
    @Resource
    private OrganizationRelationMapper organizationRelationMapper;
 
    private static Class ENTITY_CLASS = Organization.class;
 
    @Override
    public Set<Integer> getChildOrganizationIds(Integer orgId){
        Set<Integer> orgIds = new HashSet<Integer>();
        orgIds.add(orgId);
        OrganizationRelation relation = new OrganizationRelation();
        relation.setParentId(orgId);
        Organization organization = organizationMapper.selectByPrimaryKey(orgId);
        if (IS_DELETE_FALSE.equals(organization.getIsDelete())) {
            List<OrganizationRelation> organizationRelations = organizationRelationMapper.select(relation);
            for (OrganizationRelation organizationRelation : organizationRelations) {
                Set<Integer> organizationIds = getChildOrganizationIds(organizationRelation.getParentId());
                orgIds.addAll(organizationIds);
            }
        }
        return orgIds;
    }
 
 
    @Override
    public List<Organization> getOrganizationsByAreaName(Map<String, Object> parameters) {
        ValidateUtil.notNull(parameters.get("areaName"), "param.is.null");
        List<Organization> organizations = organizationMapper.getOrganizationsByAreaName(parameters);
        return organizations;
    }
 
    public PageBean queryByPageBean(PageBean pageBean){
        Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
        List<Example.Criteria> criteriaList = example.getOredCriteria();
        if(criteriaList!=null&&criteriaList.size()>0){
            for(Example.Criteria cri : criteriaList){
                cri.andNotEqualTo("isDelete","1");
            }
        }else {
            example.or().andNotEqualTo("isDelete","1");
        }
        PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize());
        List<OrganizationExp> organizationExpandList = organizationMapper.selectWithAreaNameByExample(example);
        return new PageBean(organizationExpandList);
    }
    public void addOrModify(Organization organization){
        try{
            if(organization.getId()==null){
                //新建数据,默认为未删除状态
                organization.setIsDelete("0");
                organizationMapper.insertSelective(organization);
            }else{
                organizationMapper.updateByPrimaryKeySelective(organization);
            }
        }
        catch (Exception ex){
            throw  ex;
        }
    }
 
    @Override
    public void deleteByIds(Integer... ids) {
        Organization organization = new Organization();
        organization.setIsDelete("1");
        if(ids!=null&&ids.length>0){
            if(ids.length==1){
                organization.setId(ids[0]);
                organizationMapper.updateByPrimaryKeySelective(organization);
            }else{
                Example example = new Example(ENTITY_CLASS);
                example.or().andIn("id", Arrays.asList(ids));
                organizationMapper.updateByExampleSelective(organization,example);
            }
 
        }
    }
}