<commit_before>package selling.sunshine.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.sunshine.model.selling.admin.Admin;
import common.sunshine.model.selling.agent.Agent;
import common.sunshine.model.selling.order.CustomerOrder;
import common.sunshine.model.selling.user.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import selling.sunshine.form.GoodsForm;
import common.sunshine.utils.SortRule;
import selling.sunshine.model.*;
import common.sunshine.model.selling.goods.Goods4Customer;
import common.sunshine.model.selling.goods.Thumbnail;
import common.sunshine.pagination.DataTablePage;
import common.sunshine.pagination.DataTableParam;
import selling.sunshine.service.*;
import common.sunshine.utils.ResponseCode;
import common.sunshine.utils.ResultData;
import selling.sunshine.utils.WechatConfig;
import selling.sunshine.utils.WechatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunshine on 4/8/16.
 */
@RequestMapping("/commodity")
@RestController
public class CommodityController {

    private Logger logger = LoggerFactory.getLogger(CommodityController.class);

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ToolService toolService;

    @Autowired
    private LogService logService;

    @RequestMapping(method = RequestMethod.GET, value = "/create")
    public ModelAndView create() {
        ModelAndView view = new ModelAndView();
        view.setViewName("/backend/goods/create");
        return view;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public ModelAndView create(@Valid GoodsForm form, BindingResult result, HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        if (result.hasErrors()) {
            view.setViewName("redirect:/commodity/create");
            return view;
        }

        Goods4Customer goods = new Goods4Customer(form.getName(), Double.parseDouble(form.getAgentPrice()), Double.parseDouble(form.getPrice()), form.getDescription(), form.getStandard());
        goods.setBlockFlag(form.isBlock());
        ResultData response = commodityService.createGoods4Customer(goods);
        if (response.getResponseCode() == ResponseCode.RESPONSE_OK) {
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            if (user == null) {
                view.setViewName("redirect:/commodity/create");
                return view;
            }
            Admin admin = user.getAdmin();
            BackOperationLog backOperationLog = new BackOperationLog(
                    admin.getUsername(), toolService.getIP(request), "管理员" + admin.getUsername() + "添加了一个新商品，商品名称:"
                    + form.getName());
            logService.createbackOperationLog(backOperationLog);
            view.setViewName("redirect:/commodity/overview");
            return view;
        } else {
            view.setViewName("redirect:/commodity/create");
            return view;
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/edit/{goodsId}")
    public ModelAndView edit(@PathVariable("goodsId") String goodsId) {
        ModelAndView view = new ModelAndView();
        Map<String, Object> condition = new HashMap<>();
        condition.put("goodsId", goodsId);
        ResultData resultData = commodityService.fetchGoods4Customer(condition);
        if (resultData.getResponseCode() != ResponseCode.RESPONSE_OK) {
            view.setViewName("redirect:/commodity/overview");
            return view;
        }
        Goods4Customer target = ((ArrayList<Goods4Customer>) resultData
                .getData()).get(0);
        view.addObject("goods", target);

        view.setViewName("/backend/goods/update");
        return view;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/thumbnails/{goodsId}")
    @ResponseBody
    public String thumbnails(@PathVariable("goodsId") String goodsId) {

        Map<String, Object> condition = new HashMap<>();
        condition.put("goodsId", goodsId);
        ResultData resultData = commodityService.fetchThumbnail(condition);
        List<Thumbnail> thumbnails = (List<Thumbnail>) resultData.getData();

        JSONArray resultArray = new JSONArray();
        JSONArray initialPreviewArray = new JSONArray();
        JSONArray initialPreviewConfigArray = new JSONArray();
        if (thumbnails.size() == 0) {

            resultArray.add(initialPreviewArray);
            resultArray.add(initialPreviewConfigArray);
            return resultArray.toJSONString();
        }
        for (Thumbnail thumbnail : thumbnails) {
            JSONObject initialPreviewConfigObject = new JSONObject();
            //initialPreviewArray.add("/selling" + thumbnail.getPath());<commit_msg> Changes to be committed: 	modified:   src/main/java/selling/sunshine/controller/CommodityController.java<commit_after>package selling.sunshine.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.sunshine.model.selling.admin.Admin;
import common.sunshine.model.selling.agent.Agent;
import common.sunshine.model.selling.order.CustomerOrder;
import common.sunshine.model.selling.user.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import selling.sunshine.form.GoodsForm;
import common.sunshine.utils.SortRule;
import selling.sunshine.model.*;
import common.sunshine.model.selling.goods.Goods4Customer;
import common.sunshine.model.selling.goods.Thumbnail;
import common.sunshine.pagination.DataTablePage;
import common.sunshine.pagination.DataTableParam;
import selling.sunshine.service.*;
import common.sunshine.utils.ResponseCode;
import common.sunshine.utils.ResultData;
import selling.sunshine.utils.WechatConfig;
import selling.sunshine.utils.WechatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunshine on 4/8/16.
 */
@RequestMapping("/commodity")
@RestController
public class CommodityController {

    private Logger logger = LoggerFactory.getLogger(CommodityController.class);

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ToolService toolService;

    @Autowired
    private LogService logService;

    @RequestMapping(method = RequestMethod.GET, value = "/create")
    public ModelAndView create() {
        ModelAndView view = new ModelAndView();
        view.setViewName("/backend/goods/create");
        return view;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public ModelAndView create(@Valid GoodsForm form, BindingResult result, HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        if (result.hasErrors()) {
            view.setViewName("redirect:/commodity/create");
            return view;
        }

        Goods4Customer goods = new Goods4Customer(form.getName(), Double.parseDouble(form.getAgentPrice()), Double.parseDouble(form.getPrice()), form.getDescription(), form.getStandard());
        goods.setBlockFlag(form.isBlock());
        ResultData response = commodityService.createGoods4Customer(goods);
        if (response.getResponseCode() == ResponseCode.RESPONSE_OK) {
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            if (user == null) {
                view.setViewName("redirect:/commodity/create");
                return view;
            }
            Admin admin = user.getAdmin();
            BackOperationLog backOperationLog = new BackOperationLog(
                    admin.getUsername(), toolService.getIP(request), "管理员" + admin.getUsername() + "添加了一个新商品，商品名称:"
                    + form.getName());
            logService.createbackOperationLog(backOperationLog);
            view.setViewName("redirect:/commodity/overview");
            return view;
        } else {
            view.setViewName("redirect:/commodity/create");
            return view;
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/edit/{goodsId}")
    public ModelAndView edit(@PathVariable("goodsId") String goodsId) {
        ModelAndView view = new ModelAndView();
        Map<String, Object> condition = new HashMap<>();
        condition.put("goodsId", goodsId);
        ResultData resultData = commodityService.fetchGoods4Customer(condition);
        if (resultData.getResponseCode() != ResponseCode.RESPONSE_OK) {
            view.setViewName("redirect:/commodity/overview");
            return view;
        }
        Goods4Customer target = ((ArrayList<Goods4Customer>) resultData
                .getData()).get(0);
        view.addObject("goods", target);

        view.setViewName("/backend/goods/update");
        return view;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/thumbnails/{goodsId}")
    @ResponseBody
    public String thumbnails(@PathVariable("goodsId") String goodsId) {

        Map<String, Object> condition = new HashMap<>();
        condition.put("goodsId", goodsId);
        condition.put("type", "slide");
        ResultData resultData = commodityService.fetchThumbnail(condition);
        List<Thumbnail> thumbnails = (List<Thumbnail>) resultData.getData();

        JSONArray resultArray = new JSONArray();
        JSONArray initialPreviewArray = new JSONArray();
        JSONArray initialPreviewConfigArray = new JSONArray();
        if (thumbnails.size() == 0) {

            resultArray.add(initialPreviewArray);
            resultArray.add(initialPreviewConfigArray);
            return resultArray.toJSONString();
        }
        for (Thumbnail thumbnail : thumbnails) {
            JSONObject initialPreviewConfigObject = new JSONObject();
            //initialPreviewArray.add("/selling" + thumbnail.getPath());
            initialPreviewArray.add(thumbnail.getPath());
//			initialPreviewConfigObject.put(
//					"url",
//					"/selling/commodity/delete/Thumbnail/"
//							+ thumbnail.getThumbnailId());
            initialPreviewConfigObject.put(
                    "url",
                    "/commodity/delete/Thumbnail/"
                            + thumbnail.getThumbnailId());
            initialPreviewConfigObject.put("key", thumbnail.getThumbnailId());
            initialPreviewConfigArray.add(initialPreviewConfigObject);
        }
        resultArray.add(initialPreviewArray);
        resultArray.add(initialPreviewConfigArray);
        return resultArray.toJSONString();

    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/picture/{goodsId}")
    @ResponseBody
    public String picture(@PathVariable("goodsId") String goodsId) {

        Map<String, Object> condition = new HashMap<>();
        condition.put("goodsId", goodsId);
        condition.put("type", "cover");
        ResultData resultData = commodityService.fetchThumbnail(condition);
        List<Thumbnail> thumbnails = (List<Thumbnail>) resultData.getData();

        JSONArray resultArray = new JSONArray();
        JSONArray initialPreviewArray = new JSONArray();
        JSONArray initialPreviewConfigArray = new JSONArray();
        if (thumbnails.size() == 0) {

            resultArray.add(initialPreviewArray);
            resultArray.add(initialPreviewConfigArray);
            return resultArray.toJSONString();
        }
        for (Thumbnail thumbnail : thumbnails) {
            JSONObject initialPreviewConfigObject = new JSONObject();
            //initialPreviewArray.add("/selling" + thumbnail.getPath());
{'fraction_non_alphanumeric': 0.10856883097204334, 'fraction_numerical': 0.0023486627774831426, 'mean_word_length': 3.366523321204102, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3522994', 'n_tokens_mistral': 3578, 'n_tokens_neox': 3526, 'n_words': 656}