地区

实体类

package com.toltech.phatent.persistence.entity.custom;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.toltech.framework.commons.annotation.LogField;
import com.toltech.phatent.commons.enums.BasicStatu;
import com.toltech.phatent.persistence.core.BasicEntity;

/**
 * Area
 * 
 * @author qiuyp
 * @version 1.0
 * @since 2017年8月16日下午7:27:49
 */
@DynamicInsert(true)
@DynamicUpdate(true)
@Entity(name = "t_custom_area_infos")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "phatent.Cache")
public class Area extends BasicEntity {
    private static final long serialVersionUID = 1L;
    @LogField(name = "地区名称")
    private String sname;
    @LogField(name = "地区代码")
    private Integer sncode;
    @LogField(name = "启用状态")
    private BasicStatu statu;
    @LogField(name = "所属辖区ID")
    private Integer parentId;
    @LogField(name = "所属辖区")
    private Area parent;
    @LogField(name = "下辖地区")
    private List childs;

    @Column(name = "sname")
    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Column(name = "sncode")
    public Integer getSncode() {
        return sncode;
    }

    public void setSncode(Integer sncode) {
        this.sncode = sncode;
    }

    @Column(name = "statu")
    public BasicStatu getStatu() {
        return statu;
    }

    public void setStatu(BasicStatu statu) {
        this.statu = statu;
    }

    @Column(name = "parent_id")
    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentId")
    public List getChilds() {
        return childs;
    }

    public void setChilds(List childs) {
        this.childs = childs;
    }
}

Dao

package com.toltech.phatent.persistence.dao.custom;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.toltech.framework.orm.support.Pagination;
import com.toltech.phatent.commons.enums.BasicStatu;
import com.toltech.phatent.persistence.core.BasicDao;
import com.toltech.phatent.persistence.entity.custom.Area;

/**
 * AreaDao
 *
 * @author qiuyp
 * @version 1.0
 * @since 2017年8月16日下午7:47:52
 */
@Repository
@SuppressWarnings("unchecked")
public class AreaDao extends BasicDao {
    private static final long serialVersionUID = 1L;

    public Pagination findPage(Area search, int curPage, int pageSize){
        Criteria criteria = createCriteria();
        if(search != null){
            if(StringUtils.isNotBlank(search.getSname()))
                criteria.add(Restrictions.like("sname", search.getSname(), MatchMode.ANYWHERE));
            if(search.getSncode() != null)
                criteria.add(Restrictions.eq("sncode", search.getSncode()));
            if(search.getStatu() != null)
                criteria.add(Restrictions.eq("statu", search.getStatu()));
            if(search.getParentId() != null)
                criteria.add(Restrictions.eq("parentId", search.getParentId()));
        }
        criteria.addOrder(Order.desc("id"));
        return findPage(criteria, curPage, pageSize);
    }
    
    public List findParents(Integer parentId){
        Criteria criteria = createCriteria();
        if(parentId == null){
            criteria.add(Restrictions.isNull("parentId"));
        } else {
            criteria.add(Restrictions.eq("parentId", parentId));
        }
        criteria.add(Restrictions.eq("statu", BasicStatu.ENABLED));
        criteria.addOrder(Order.desc("id"));
        return criteria.list();
    }
    
    public Integer findId(Integer sncode){
        Criteria criteria = createCriteria();
        criteria.add(Restrictions.eq("sncode", sncode));
        criteria.setProjection(Projections.id());
        return (Integer) criteria.uniqueResult();
    }
    
    public long findCount(Integer sncode){
        Criteria criteria = createCriteria();
        criteria.add(Restrictions.eq("sncode", sncode));
        criteria.setProjection(Projections.rowCount());
        return (Long) criteria.uniqueResult();
    }
}

service

package com.toltech.phatent.service.custom;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.toltech.framework.orm.support.Pagination;
import com.toltech.phatent.persistence.dao.custom.AreaDao;
import com.toltech.phatent.persistence.entity.custom.Area;
import com.toltech.phatent.service.core.BasicServiceBean;

/**
 * AreaServiceBean
 * @author qiuyp
 * @version 1.0
 * @since 2017年8月17日上午9:26:01
 */
@Service
public class AreaServiceBean extends BasicServiceBean {
    private static final long serialVersionUID = 1L;
    @Resource
    private AreaDao areaDao;

    @Override
    public AreaDao getEntityDao() {
        return areaDao;
    }

    public Pagination findPage(Area search, int curPage, int pageSize){
        return areaDao.findPage(search, curPage, pageSize);
    }
    
    public List findParents(Integer parentId){
        return areaDao.findParents(parentId);
    }
    
    public long findCount(Integer sncode){
        return areaDao.findCount(sncode);
    }
}

controller

package com.toltech.phatent.web.action.custom;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.toltech.framework.orm.support.Pagination;
import com.toltech.phatent.commons.bean.UserBean;
import com.toltech.phatent.commons.enums.BasicStatu;
import com.toltech.phatent.commons.enums.FlagStatu;
import com.toltech.phatent.commons.enums.OperateType;
import com.toltech.phatent.persistence.entity.custom.Area;
import com.toltech.phatent.service.custom.AreaServiceBean;
import com.toltech.phatent.web.core.BasicAction;

/**
 * AreaAction
 *
 * @author qiuyp
 * @version 1.0
 * @since 2017年8月17日上午9:25:18
 */
@Controller
@RequestMapping("custom/area")
public class AreaAction extends BasicAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private AreaServiceBean areaServiceBean;

    @RequestMapping(INDEX)
    public void index(ModelMap map) {
        BasicStatu[] status = BasicStatu.values();
        map.put("status", status);
        List parents = areaServiceBean.findParents(null);
        map.put("parents", parents);
    }

    @ResponseBody
    @RequestMapping(QUERY)
    public JSONObject query(Area search, int page, int rows) {
        JSONObject result = new JSONObject();
        Pagination datas = areaServiceBean.findPage(search, page, rows);
        JSONArray array = new JSONArray();
        if (datas.getRowCount() > 0) {
            for (Area entity : datas.getResults()) {
                JSONObject object = new JSONObject();
                object.put("id", entity.getId());
                object.put("sname", entity.getSname());
                object.put("sncode", entity.getSncode());
                object.put("statu", entity.getStatu());
                object.put("parentId", entity.getParentId());
                if (entity.getParent() != null) {
                    object.put("parent", entity.getParent().getSname());
                } else {
                    object.put("parent", "-");
                }
                object.put("createdAt", entity.getCreatedAt());
                object.put("updatedAt", entity.getUpdatedAt());
                array.add(object);
            }
        }
        result.put("rows", array);
        result.put("total", datas.getRowCount());
        return result;
    }

    @ResponseBody
    @RequestMapping(CREATE)
    public JSONObject create(Area result, HttpServletRequest request) {
        JSONObject object = new JSONObject();
        if (result.getSncode() == null || StringUtils.isBlank(result.getSname())) {
            object.put("success", false);
            object.put("message", "名称或代码不能为空");
            return object;
        }
        long count = areaServiceBean.findCount(result.getSncode());
        if (count == 0) {
            UserBean user = getCurrentUser(request);
            result.setCreater(user.getUserName());
            result.setUpdater(user.getUserName());
            areaServiceBean.save(result);
            addMessage(request, null, OperateType.CREATE, FlagStatu.SUCCESS, "地区");
            object.put("success", true);
            object.put("message", "数据添加成功");
        } else {
            addMessage(request, "数据添加重复", OperateType.CREATE, FlagStatu.FAILURE, "地区");
            object.put("success", false);
            object.put("message", "数据添加重复");
        }
        return object;
    }

    @ResponseBody
    @RequestMapping(UPDATE)
    public JSONObject update(Area result, HttpServletRequest request) {
        JSONObject object = new JSONObject();
        if (result.getSncode() == null || StringUtils.isBlank(result.getSname())) {
            object.put("success", false);
            object.put("message", "名称或代码不能为空");
            return object;
        }
        if (result.getId() != null) {
            long count = areaServiceBean.findCount(result.getSncode());
            if (count < 2) {
                UserBean user = getCurrentUser(request);
                result.setUpdater(user.getUserName());
                String[] properties = {"sname", "sncode", "statu", "parentId", "updater"};
                areaServiceBean.update(result.getId(), result, properties);
                addMessage(request, null, OperateType.UPDATE, FlagStatu.SUCCESS, "地区");
                object.put("success", true);
                object.put("message", "数据修改成功");
            } else {
                addMessage(request, "数据修改重复", OperateType.UPDATE, FlagStatu.FAILURE, "地区");
                object.put("success", false);
                object.put("message", "数据修改重复");
            }
        } else {
            addMessage(request, "缺少主键", OperateType.UPDATE, FlagStatu.FAILURE, "地区");
            object.put("success", false);
            object.put("message", "数据缺少主键");
        }
        return object;
    }
}

你可能感兴趣的:(地区)