package com.j1.mai.action;

import com.github.pagehelper.PageInfo;
import com.j1.app.mysql.model.ApproveDetail;
import com.j1.app.mysql.model.ApproveGoods;
import com.j1.app.mysql.model.ApproveSort;
import com.j1.base.dto.ServiceMessage;
import com.j1.base.type.MsgStatus;
import com.j1.mai.model.common.SoaApiBaseAction;
import com.j1.mai.util.NumericUtil;
import com.j1.mai.util.StringUtil;
import com.j1.soa.common.ResultMsg;
import com.j1.soa.resource.mysql.app.api.ApproveDetailService;
import com.j1.soa.resource.mysql.app.api.ApproveGoodsService;
import com.j1.soa.resource.mysql.app.api.ApproveSortService;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 *
 * @ClassName: ApproveAction
 * @Description: 钟意Action
 * @author chenxu chenxu_j1_com
 * @date 2015年8月18日 下午2:49:13
 * @since 3.5
 *
 */
@Controller
@Scope("request")
@RequestMapping("/approve")
public class ApproveAction extends SoaApiBaseAction {

    static Logger LOG = Logger.getLogger(ApproveAction.class);
    @Autowired
    private ApproveDetailService approveDetailService;
    @Autowired
    private ApproveGoodsService approveGoodsService;
    @Autowired
    private ApproveSortService approveSortService;
/*    @Autowired
    private ApproveDetailService approveDetailService;
    @Autowired
    private PageIndexImgService pageIndexImgService;*/

    /**
     *
     * @Title: findByApproveAndSort
     * @Description:  根据分类ID查找
     *        内容包括:
     *             钟意类别所包含的所有商品列表
     * @param @param request
     * @param @param response
     * @param @param approveId 钟意ID
     * @param @param sortId  分类ID
     * @return void 返回类型
     * @throws
     */
    @RequestMapping("/sortChange")
    public void findByApproveAndSort(HttpServletRequest request,
            HttpServletResponse response,
            /*@RequestParam(value = "approveId", required = false) String approveId,*/
            @RequestParam(value = "sortId", required = false) String sortId,
            @RequestParam(value = "pnum", required = false) String pnum,
            @RequestParam(value = "psize", required = false) String psize) {
            if (StringUtil.isEmpty(sortId)) {
                this.setResultInfo(MsgStatus.PARAMS_ERROR.getCode(), ResultMsg.Common.QUERY_FAILURE)
                .write(request, response);
                return;
            }
            ApproveGoods findGoods = new ApproveGoods();
            findGoods.setApproveSortId(Integer.parseInt(sortId));
            findGoods.setCurrentPage(NumericUtil.parseInt(pnum, 1));
            findGoods.setLimitPage(NumericUtil.parseInt(psize, 20));
            ServiceMessage<PageInfo<ApproveGoods>> goodsList = null;
            try {
                goodsList = approveGoodsService.getApproveGoodsBySortId(findGoods);
                if(goodsList != null && goodsList.getResult() != null) {
                    this._result.put("goodsList", goodsList.getResult().getList());
                    this._result.put("totalPage", goodsList.getResult().getPages());
                    this._result.put("pnum", NumericUtil.parseInt(pnum, 1));
                    this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK)
                    .write(request, response);
                } else {
                    this.setResultInfo(MsgStatus.NO_RESULT.getCode(), ResultMsg.Common.QUERY_FAILURE)
                     .write(request, response);
                }

            } catch (Exception e) {
                LOG.error("approveGoodsService.getApproveGoodsBySortId:" + e);
                this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
                 .write(request, response);
            } finally {
                if(goodsList != null) {
                    LOG.debug("approveSortService.getApproveSortByApproveId:" + goodsList.getStatus());
                }
            }
    }

    /**
     *
     * @Title: findByApprove
     * @Description:  根据钟意ID和分类ID查找
     *        内容包括:
     *             钟意包含的所有类别列表
     *             缺省第一个类别所包含的所有商品列表
     * @param request
     * @param response
     * @param approveId 一级分类id
     * @param sortId 二级分类id
     * @return void 返回类型
     * @throws
     */
    @RequestMapping("/approveChange")
    public void findByApprove(HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "approveId", required = false) String approveId,
            @RequestParam(value = "sortId", required = false) String sortId,
            @RequestParam(value = "pnum", required = false) String pnum,
            @RequestParam(value = "psize", required = false) String psize) {

        try {//根据id查询促销标语和背景图片
            ServiceMessage<ApproveDetail> message = approveDetailService.getApproveDetailById(NumericUtil.parseInt(approveId, 1));
            if(message.getStatus()==MsgStatus.NORMAL||message.getStatus()==MsgStatus.NO_RESULT) {
                this._result.put("title", message.getResult()!=null?message.getResult().getApprovePromoteDesc():"");
                this._result.put("titleColor", message.getResult()!=null?message.getResult().getColor():"");
            }else {
                this._result.put("title", "");
                this._result.put("titleColor","");
            }
        } catch (Exception e) {
            LOG.error("approveDetailService.getApproveDetailById:" + e);
        }
        //根据钟意ID查询所有钟意类别列表
        ApproveSort findSort = new ApproveSort();
        //设置查询条件 ApproveId
        findSort.setApproveId(NumericUtil.parseInt(approveId, 1));
        //根据条件查询
        ServiceMessage<List<ApproveSort>> sortList = null;
        try {
            sortList = approveSortService.getApproveSortByApproveId(findSort);
            /*this._result.setObjData(sortList);*/
            if(sortList != null && sortList.getResult() != null) {
                this._result.put("sortList", sortList.getResult());
            } else {
                this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
                 .write(request, response);
                return;
            }
        } catch (Exception e) {
            LOG.error("approveSortService.getApproveSortByApproveId:" + e);
            this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
             .write(request, response);
            return;
        } finally {
            if(sortList != null) {
                LOG.debug("approveSortService.getApproveSortByApproveId:" + sortList.getStatus());
            } 

        }
        //结果中获取排序位置为为0的sort,并根据sortId查询此sort下所有商品列表
        Integer firstSortId = null;
        if (StringUtil.isEmpty(sortId)) {
            firstSortId=findFirstFromList(sortList.getResult());
        }else {
            firstSortId=Integer.parseInt(sortId);
        }

        if(firstSortId != null) {//有值才进行下一步查询
            ApproveGoods findGoods = new ApproveGoods();
            findGoods.setApproveSortId(firstSortId);
            findGoods.setCurrentPage(NumericUtil.parseInt(pnum, 1));
            findGoods.setLimitPage(NumericUtil.parseInt(psize, 20));
            ServiceMessage<PageInfo<ApproveGoods>> goodsList = null;
            try {
                goodsList = approveGoodsService.getApproveGoodsBySortId(findGoods);
                if(goodsList != null && goodsList.getResult() != null) {
                    this._result.put("goodsList", goodsList.getResult().getList());
                    this._result.put("totalPage", goodsList.getResult().getPages());
                    this._result.put("pnum", NumericUtil.parseInt(pnum, 1));
                }else {
                    this._result.put("goodsList", new JSONArray());
                    this._result.put("totalPage", 0);
                    this._result.put("pnum", NumericUtil.parseInt(pnum, 1));
                }

            } catch (Exception e) {
                LOG.error("approveGoodsService.getApproveGoodsBySortId:" + e);
                this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
                 .write(request, response);
            }

        }
        this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK).write(request, response);
    }

    /**
     *
     * @Title: findByApproveWithPage
     * @Description:   分页查询(针对分类1)
     * @param @param request
     * @param @param response
     * @param @param approveId  设定文件
     * @return void 返回类型
     * @throws
     */
    @RequestMapping("/approveByPage")
    public void findByApproveWithPage(HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "approveId", required = false) String approveId,
            @RequestParam(value = "pnum", required = false) String pnum,
            @RequestParam(value = "psize", required = false) String psize) {

        ApproveSort findSort = new ApproveSort();
        //设置查询条件 ApproveId
        findSort.setApproveId(NumericUtil.parseInt(approveId, 1));
        //根据条件查询
        ServiceMessage<List<ApproveSort>> sortList = null;
        try {
            sortList = approveSortService.getApproveSortByApproveId(findSort);
            /*this._result.setObjData(sortList);*/
            if(sortList != null && sortList.getResult() != null) {
                this._result.put("sortList", sortList.getResult());
            } else {
                this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
                 .write(request, response);
                return;
            }
        } catch (Exception e) {
            LOG.error("approveSortService.getApproveSortByApproveId:" + e);
            this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
             .write(request, response);
            return;
        } finally {
            if(sortList != null) {
                LOG.debug("approveSortService.getApproveSortByApproveId:" + sortList.getStatus());
            } 

        }

        ApproveGoods findGoods = new ApproveGoods();
        findGoods.setApproveId(NumericUtil.parseInt(approveId, 1));
        findGoods.setCurrentPage(NumericUtil.parseInt(pnum, 1));
        findGoods.setLimitPage(NumericUtil.parseInt(psize, 20));
        LOG.info("CurrentPage:" + findGoods.getCurrentPage());
        LOG.info("limitPage:" + findGoods.getLimitPage());
        ServiceMessage<PageInfo<ApproveGoods>> goodsList = null;
        try {
            goodsList = approveGoodsService.getApproveGoodsByApproveId(findGoods);
            if(goodsList != null && goodsList.getResult() != null) {
                this._result.put("goodsList", goodsList.getResult().getList());
                this._result.put("totalPage", goodsList.getResult().getPages());
                this._result.put("pnum", NumericUtil.parseInt(pnum, 1));
                this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK)
                .write(request, response);
            } else {
                this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
                 .write(request, response);
                return;
            }
        } catch (Exception e) {
            LOG.error("approveSortService.getApproveSortByApproveId:" + e);
            this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE)
             .write(request, response);
        } finally {
            if(goodsList != null) {
                LOG.debug("approveGoodsService.getApproveGoodsByApproveId:" + goodsList.getStatus());
            } 

        }
    }

    /**
     *
     * @Title: findFirstFromList
     * @Description:   查询list列表中第一个位置ID
     * @param @param lists
     * @param @return  设定文件
     * @return Integer 返回类型
     * @throws
     */
    private Integer findFirstFromList(List<ApproveSort> lists) {
        if(lists == null || lists.size() < 1) {
            return null;
        }
        for(ApproveSort s : lists) {
            if(s != null) {
                return s.getId();
            }
        }
        return null;
    }

}

钟意Action的更多相关文章

  1. redux-amrc:用更少的代码发起异步 action

    很多人说 Redux 代码多,开发效率低.其实 Redux 是可以灵活使用以及拓展的,经过充分定制的 Redux 其实写不了几行代码.今天先介绍一个很好用的 Redux 拓展-- redux-amrc ...

  2. 尝试asp.net mvc 基于controller action 方式权限控制方案可行性

    微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...

  3. ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results

    原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...

  4. java中Action层、Service层和Dao层的功能区分

    Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...

  5. SpringMVC的Action在同一时间里只允许同一个浏览器的单次进入?

    最近用SpringMVC写了一个很简单的测试程序,代码如下: @Controller public class LongTimeTaskController { @RequestMapping(val ...

  6. No result defined for action com.lk.IndexAction and result success

    意图访问一个 /es/index.action 竟然出现: [SAE ] ERROR [05-11 13:54:32] [http-80-5] com.opensymphony.xwork2.util ...

  7. 实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过App_Start中的FilterConfig来实现的过滤器注册是全局的,也就是整个应用程序都会使用的,针对单独的Fi ...

  8. ASP.NET MVC 5调用其他Action

    引用代码: @Html.Action("Index", "BaseData", new { d = "variety" }) 后台获取参数: ...

  9. C#基础知识六之委托(delegate、Action、Func、predicate)

    1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...

随机推荐

  1. 纯css3实现的创意图片放大镜

    今天要给大家分享的的一款用纯css3实现的图片放大镜特效.页面打开五个小图显示于页面.当鼠标经过图片时,当前图片以灰色背景图的形式展示.效果非常不错. 在线预览   源码下载 实现的代码: html代 ...

  2. 在input中回车后页面提交导致出现HTTP 错误 405.0 - Method Not Allowed

    前些时间在做一个搜索功能时发现一个比較有意思的现象,场景是这种:在一个模态窗体中是一个订单列表.页面的顶部有若干个状态筛选框和一个搜索keyword输入框,当焦点在keyword输入框时按回车,本来是 ...

  3. 【Visual Studio】控制台程序运行时一闪而过

    问题:写一个C#控制台程序,运行时控制台一闪而过.程序结束.无法看清控制台输出的结果. 办法: 方法一二三都有人写过百度经验了:怎么解决VS运行程序一闪而过 其实还有一种办法,就是把控制台程序改为Wi ...

  4. 【WPF】对话框/消息弹窗

    非模式对话框 需求:弹窗是非模式对话框,即可以多个弹窗弹出,且弹窗后面的窗体可以被操作,不会被锁定. 自定义的窗体Window实现以下步骤: 在C#代码中弹出窗体时,使用 window.Show() ...

  5. 一站式学习Wireshark(八):应用Wireshark过滤条件抓取特定数据流

    应用抓包过滤,选择Capture | Options,扩展窗口查看到Capture Filter栏.双击选定的接口,如下图所示,弹出Edit Interface Settints窗口. 下图显示了Ed ...

  6. Oracle数据误删除的恢复操作

    flashbackup 闪回操作: 1. 打开表的闪回功能: alter table dw_stg.fm_user_play_d enable row movement; 2. 查询要闪回的表的记录信 ...

  7. Mac升级yosemite后无法登陆问题

    Mac升级yosemite后无法登陆问题         今天心血来潮准备玩玩最新的苹果系统10.10,代号是yosemite.去官网申请了beta版的測试资格,然后在app store下载了一晚上得 ...

  8. html精灵技术(用来显示图片的某个区域)

    .left .left_down li.a.left-down-pic{display:block;width:50px;height:50px;background:url(images/app_i ...

  9. W/MessageQueue: Handler (android.os.Handler) {4241f8f8} sending message to a Handler on a dead thread

    缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的 稍微纤细一点儿的信息是: Handler (andro ...

  10. 【转】【VC】VC程序运行时间测试函数

    1:Sleep函数 使用: sleep(1000),在Windows和Linux下1000代表的含义并不相同,Windows下的表示1000毫秒,也就是1秒钟: Linux下表示1000秒,Linux ...