com.etiansoft.haier.service.ManagerService.java Source code

Java tutorial

Introduction

Here is the source code for com.etiansoft.haier.service.ManagerService.java

Source

package com.etiansoft.haier.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.etiansoft.DataTablePage;
import com.etiansoft.Result;
import com.etiansoft.haier.constants.Constants;
import com.etiansoft.haier.po.Address;
import com.etiansoft.haier.po.Logistics;
import com.etiansoft.haier.po.Merchant;
import com.etiansoft.haier.po.MerchantPicture;
import com.etiansoft.haier.po.Order;
import com.etiansoft.haier.po.OrderItem;
import com.etiansoft.haier.po.Mpromotion;
import com.etiansoft.haier.po.SensitivRecord;
import com.etiansoft.haier.util.UdsUtil;
import com.etiansoft.haier.vo.GoodsVo;
import com.etiansoft.haier.vo.MerchantVo;
import com.etiansoft.tools.common.DateTool;
import com.etiansoft.tools.common.MD5;
import com.etiansoft.tools.web.ServletContextProvider;

/**
 * 
 * @ClassName: 
 * @Description: TODO
 * @author  (chenxiaoqiang@ablecloud.cn)
 * @date 2016-3-2 ?11:25:12
 * 
 */
@Service
@SuppressWarnings("unchecked")
public class ManagerService {

    @Autowired
    private UdsService udsService;
    @Autowired
    private UdsUtil udsUtil;
    @Value("${haierRequestUrl}")
    private String haierRequestUrl;

    private final String tableName = "merchant";
    private final String idName = "merchant_id_";

    /**
     * 
     * @Title: ???
     * @Description: TODO
     * @param @param page
     * @param @param name
     * @param @param phone
     * @param @param status
     * @param @return
     * @param @throws Exception
     * @return DataTablePage
     * @throws
     */
    public DataTablePage getData(DataTablePage page, String name, String phone, String status, String merchant_num,
            String order_id) throws Exception {
        DataTablePage dataTable = null;

        dataTable = udsService.findPage(page, Merchant.class, MerchantVo.class, tableName, "create_date", "desc",
                makeParam(name, phone, status, merchant_num));

        List<MerchantVo> datas = (List<MerchantVo>) dataTable.getAaData();

        for (MerchantVo dataMerchant : datas) {
            Map<String, Object> mapm = new HashMap<String, Object>();
            mapm.put("merchant_id", dataMerchant.getMerchantId());
            List<Order> listm = (List<Order>) udsService.findByTimeForOrder2("order", Order.class, mapm);

            BigDecimal num = new BigDecimal(0);

            if (listm != null && listm.size() > 0) {
                for (Order or : listm) {
                    if (or.getStatus().intValue() == 2 || or.getStatus().intValue() == 3
                            || or.getStatus().intValue() == 4 || or.getStatus().intValue() == 5) {
                        num = num.add(new BigDecimal(or.getFavored_amount()).add(new BigDecimal(or.getFreight())));
                    }
                }
            }
            dataMerchant.setSaleVolum(num.setScale(2, RoundingMode.HALF_UP).doubleValue());
            Mpromotion promotion = (Mpromotion) udsService.findById("mpromotion", "merchant_id_",
                    dataMerchant.getMerchantId(), Mpromotion.class);
            if (promotion != null) {
                dataMerchant.setPromotion_sort(promotion.getSort());
                dataMerchant.setPromotion_tag(promotion.getPromotion_tag());
            }

        }

        return dataTable;

    }

    public DataTablePage getHistoryData(DataTablePage page, String code) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("merchant_id_", code);
        return udsService.findPage(page, SensitivRecord.class, null, "sensitiv_record", "option_time", "desc", map);
    }

    private Map<String, Object> makeParam(String name, String phone, String status, String merchant_num) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("remove", "0");
        map.put("admin", "0");
        if (StringUtils.isNotEmpty(name)) {
            map.put("name", name);
        }
        if (StringUtils.isNotEmpty(phone)) {
            map.put("phone", phone);
        }
        if (!StringUtils.equals("-1", status)) {
            map.put("status", status);
        }

        if (StringUtils.isNotEmpty(merchant_num)) {
            map.put("merchant_num", Integer.valueOf(merchant_num));
        }

        return map;
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @return
     * @param @throws Exception
     * @return List<Merchant>
     * @throws
     */
    public List<Merchant> findAll() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("remove", "0");
        map.put("admin", "0");
        return (List<Merchant>) udsService.findByParams(tableName, Merchant.class, map);
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @return
     * @param @throws Exception
     * @return List<Merchant>
     * @throws
     */
    public Merchant findOne(String code) throws Exception {

        return (Merchant) udsService.findById(tableName, idName, code, Merchant.class);
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param manager
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result save(Merchant manager) throws Exception {
        HttpURLConnection emailHttp = udsUtil.connect("validation_merchant?email=" + manager.getEmail(), "POST");
        String emailResult = udsUtil.readFromHttp(emailHttp);
        JSONObject emailJson = JSONObject.fromObject(emailResult);
        if (StringUtils.equals("emailIsExists", emailJson.getString("result"))) {
            return Result.error("!");
        }
        HttpURLConnection phoneHttp = udsUtil.connect("validation_merchant?phone=" + manager.getService_phone(),
                "POST");
        String phoneResult = udsUtil.readFromHttp(phoneHttp);
        JSONObject phoneJson = JSONObject.fromObject(phoneResult);
        if (StringUtils.equals("phoneIsExists", phoneJson.getString("result"))) {
            return Result.error("?!");
        }
        manager.setMerchant_id_(UUID.randomUUID().toString());
        manager.setRemove(0);
        manager.setAdmin(0);
        String password = manager.getPassword();
        if (StringUtils.isNotEmpty(password)) {
            manager.setPassword(MD5.md5(password));
        }
        String result = udsService.save(manager, tableName, idName);
        if (!StringUtils.equals("success", result)) {
            return Result.error("!");
        }
        return Result.SUCCESS;
    }

    private String formatAddress(String orgAddr) {
        String ret = orgAddr.replace("p", "").replace("c", "").replace("i", "").replace("#", "");

        return ret;
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param manager
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result update(Merchant manager, String name, String servicePhone, String servicePhoneTime,
            String address, String reminder) throws Exception {
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, manager.getMerchant_id_(),
                Merchant.class);
        SensitivRecord sensitivRecord = new SensitivRecord();
        sensitivRecord.setMerchant_id_(manager.getMerchant_id_());
        //      HttpURLConnection emailHttp = udsUtil.connect("validation_merchant?email=" + manager.getEmail() + "&merchantId=" + manager.getMerchant_id_(), "POST");
        //      String emailResult = udsUtil.readFromHttp(emailHttp);
        //      JSONObject emailJson = JSONObject.fromObject(emailResult);
        //      if (StringUtils.equals("emailIsExists", emailJson.getString("result"))) {
        //         return Result.error("!");
        //      }
        //      HttpURLConnection phoneHttp = udsUtil.connect("validation_merchant?phone=" + manager.getServicePhone() + "&merchantId=" + manager.getMerchant_id_(), "POST");
        //      String phoneResult = udsUtil.readFromHttp(phoneHttp);
        //      JSONObject phoneJson = JSONObject.fromObject(phoneResult);
        //      if (StringUtils.equals("phoneIsExists", phoneJson.getString("result"))) {
        //         return Result.error("?!");
        //      }
        StringBuilder operaRecord = new StringBuilder();
        if (!StringUtils.equals(merchant.getName(), name)) {
            operaRecord.append("??").append(merchant.getName()).append("").append(name)
                    .append("\n");

            sensitivRecord.setSensitiv_record_id(UUID.randomUUID().toString());

            sensitivRecord.setOption_name("??");
            sensitivRecord.setOriginal_content(merchant.getName());
            sensitivRecord.setUpdate_content(name);
            sensitivRecord.setOption_time(DateTool.currentTimeDisplay());
            String result2 = udsService.update(sensitivRecord, "sensitiv_record", "sensitiv_record_id");
            if (!StringUtils.equals("success", result2)) {
                return Result.error("??!");
            }

        }
        if (!StringUtils.equals(merchant.getService_phone(), servicePhone)) {
            operaRecord.append("??").append(merchant.getService_phone()).append("")
                    .append(servicePhone).append("\n");

            sensitivRecord.setSensitiv_record_id(UUID.randomUUID().toString());
            sensitivRecord.setOption_name("??");
            sensitivRecord.setOriginal_content(merchant.getService_phone());
            sensitivRecord.setUpdate_content(servicePhone);
            sensitivRecord.setOption_time(DateTool.currentTimeDisplay());
            String result2 = udsService.update(sensitivRecord, "sensitiv_record", "sensitiv_record_id");
            if (!StringUtils.equals("success", result2)) {
                return Result.error("??!");
            }

        }
        if (!StringUtils.equals(merchant.getService_phone_time(), servicePhoneTime)) {
            operaRecord.append("???").append(merchant.getService_phone_time())
                    .append("").append(servicePhoneTime).append("\n");

            sensitivRecord.setSensitiv_record_id(UUID.randomUUID().toString());
            sensitivRecord.setOption_name("???");
            sensitivRecord.setOriginal_content(merchant.getService_phone_time());
            sensitivRecord.setUpdate_content(servicePhoneTime);
            sensitivRecord.setOption_time(DateTool.currentTimeDisplay());
            String result2 = udsService.update(sensitivRecord, "sensitiv_record", "sensitiv_record_id");
            if (!StringUtils.equals("success", result2)) {
                return Result.error("??!");
            }

        }
        if (!StringUtils.equals(merchant.getAddress(), address)) {
            operaRecord.append("?").append(merchant.getAddress()).append("").append(address)
                    .append("\n");

            sensitivRecord.setSensitiv_record_id(UUID.randomUUID().toString());
            sensitivRecord.setOption_name("?");
            sensitivRecord.setOriginal_content(formatAddress(merchant.getAddress()));
            sensitivRecord.setUpdate_content(formatAddress(address));
            sensitivRecord.setOption_time(DateTool.currentTimeDisplay());
            String result2 = udsService.update(sensitivRecord, "sensitiv_record", "sensitiv_record_id");
            if (!StringUtils.equals("success", result2)) {
                return Result.error("??!");
            }

        }
        merchant.setOpera_record(operaRecord.toString());

        //      merchant.setEmail(manager.getEmail());

        if (name != null) {
            merchant.setName(name);
        }
        if (servicePhone != null) {
            merchant.setService_phone(servicePhone);
        }

        if (servicePhoneTime != null) {
            merchant.setService_phone_time(servicePhoneTime);
        }
        if (address != null) {
            merchant.setAddress(address);
        }
        if (reminder != null) {
            merchant.setReminder(reminder);
        }

        String result = udsService.update(merchant, tableName, idName);
        if (!StringUtils.equals("success", result)) {
            return Result.error("?!");
        }
        ServletContextProvider.getSession().setAttribute(Constants.LOGIN_MANAGER, merchant);

        return Result.SUCCESS;
    }

    /**
     * 
     * @Title: ???
     * @Description: TODO
     * @param @param merchant_id_
     * @param @return
     * @param @throws Exception
     * @return 
     * @throws
     */
    public DataTablePage getSensitivAllInfo(DataTablePage page, String code) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(code)) {
            map.put("merchant_id_", code);
        }
        DataTablePage dataTable = udsService.findPage(page, SensitivRecord.class, null, "sensitiv_record",
                "option_time", "desc", map);
        return dataTable;
    }

    /**
     * 
     * @Title: ?
     * @Description: TODO
     * @param @param obj
     * @param @param tableName
     * @param @param idName
     * @param @return
     * @param @throws Exception
     * @return String
     * @throws
     */
    public Result updateStatus(Merchant manager) throws Exception {

        Merchant merchant = (Merchant) udsService.findById(tableName, idName, manager.getMerchant_id_(),
                Merchant.class);

        merchant.setStatus(1);
        String result = udsService.update(merchant, tableName, idName);
        if (!StringUtils.equals("success", result)) {
            return Result.error("?!");
        }
        ServletContextProvider.getSession().setAttribute(Constants.LOGIN_MANAGER, merchant);
        return Result.SUCCESS;
    }

    /**
     * 
     * @Title: ?
     * @Description: TODO
     * @param @param manager
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result updateMerchantSenInfo(String code, String operatorRealName, String idNumber,
            String registeredEnterpriseName, String businessLicense) throws Exception {
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, code, Merchant.class);

        if (operatorRealName != null) {
            merchant.setOperator_real_name(operatorRealName);
        }
        if (operatorRealName != null) {
            merchant.setId_number(idNumber);
            ;
        }

        if (registeredEnterpriseName != null) {
            merchant.setRegistered_enterpris(registeredEnterpriseName);
            ;
        }
        if (businessLicense != null) {
            merchant.setBusiness_license(businessLicense);
            ;
        }

        String result = udsService.update(merchant, tableName, idName);
        if (!StringUtils.equals("success", result)) {
            return Result.error("!");
        }
        ServletContextProvider.getSession().setAttribute(Constants.LOGIN_MANAGER, merchant);
        return Result.SUCCESS;
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param code
     * @param @param 
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public String getPicturesUrl(List<MerchantPicture> merchantPictures) throws Exception {
        StringBuilder urls = new StringBuilder();
        if (merchantPictures != null) {
            for (MerchantPicture merchantPicture : merchantPictures) {
                urls.append(merchantPicture.getUrl()).append(",");
            }
            urls.delete(urls.length() - 1, urls.length());
        }
        return urls.toString();
    }

    /**
     * 
     * @Title: ??
     * @Description: TODO
     * @param @param code
     * @param @param password
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result resetPassword(String code, String password) throws Exception {
        if (StringUtils.isEmpty(code)) {
            return Result.error("?!");
        }
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, code, Merchant.class);
        merchant.setPassword(MD5.md5(password));
        String result = udsService.update(merchant, tableName, idName);
        if (!StringUtils.equals("success", result)) {
            return Result.error("??!");
        }
        return Result.SUCCESS;
    }

    public Integer managerContrast(Merchant manager) {
        if (manager != null) {
            String merchantId = manager.getMerchant_id_();
            if (StringUtils.isNotEmpty(merchantId)) {
                Merchant merchant = (Merchant) udsService.findById(tableName, idName, merchantId, Merchant.class);
                if (merchant != null) {
                    if (!StringUtils.equals(merchant.getName(), manager.getName())) {
                        return 0;
                    } else if (!StringUtils.equals(merchant.getPassword(), manager.getPassword())) {
                        return 0;
                    } else if (!StringUtils.equals(merchant.getEmail(), manager.getEmail())) {
                        return 0;
                    } else if (!StringUtils.equals(merchant.getService_phone(), manager.getService_phone())) {
                        return 0;
                    } else if (!StringUtils.equals(merchant.getAddress(), manager.getAddress())) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param code
     * @param @param userId
     * @param @throws Exception
     * @return void
     * @throws
     */
    public void appred(String code, String userId) throws Exception {
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, code, Merchant.class);
        try {
            String accessToken = StringUtils
                    .lowerCase(MD5.md5(merchant.getUser_id() + "+" + merchant.getMerchant_id_()));
            Map<String, String> map = new HashMap<String, String>();
            map.put("userId", merchant.getUser_id());
            map.put("shopId", merchant.getMerchant_id_());
            map.put("accessToken", accessToken);
            String result = udsUtil.post(haierRequestUrl + "/api/mall/regsuc", null, map);
            JSONObject json = JSONObject.fromObject(result);
            String retCode = json.get("retCode").toString();
            if (StringUtils.isNotEmpty(retCode) && StringUtils.equals("0", retCode)) {
                merchant.setStatus(1);
                udsService.update(merchant, tableName, idName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param code
     * @param @param reason
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result reject(String code, String reason) throws Exception {
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, code, Merchant.class);
        merchant.setStatus(2);
        merchant.setReson(reason);
        String result = udsService.update(merchant, tableName, idName);
        if (StringUtils.isNotEmpty(result) && StringUtils.equals("success", result)) {
            return Result.SUCCESS;
        }
        return Result.error("");
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param code
     * @param @param reason
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result closeMec(String code, String reason) throws Exception {
        Merchant merchant = (Merchant) udsService.findById(tableName, idName, code, Merchant.class);
        merchant.setStatus(3);
        merchant.setReson(reason);
        String result = udsService.update(merchant, tableName, idName);
        if (StringUtils.isNotEmpty(result) && StringUtils.equals("success", result)) {
            return Result.SUCCESS;
        }
        return Result.error("");
    }

    /**
     * 
     * @Title: ?
     * @Description: TODO
     * @param @param status
     * @param @return
     * @param @throws Exception
     * @return Integer
     * @throws
     */
    public Integer getSizeByStatus(String status) throws Exception {
        int count = 0;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", status);
        List<?> merchants = udsService.findByParams(tableName, Merchant.class, map);
        if (merchants != null && merchants.size() > 0) {
            count = merchants.size();
        }
        return count;
    }

    /**
     * 
     * @Title: 
     * @Description: TODO
     * @param @param code
     * @param @param reason
     * @param @return
     * @param @throws Exception
     * @return Result
     * @throws
     */
    public Result deleteMec(String code) throws Exception {
        String result = udsService.doDelete(tableName, idName, code, null);
        if (StringUtils.isNotEmpty(result) && StringUtils.equals("success", result)) {
            return Result.SUCCESS;
        }
        return Result.error("");
    }
}