com.carfinance.module.common.service.CommonService.java Source code

Java tutorial

Introduction

Here is the source code for com.carfinance.module.common.service.CommonService.java

Source

package com.carfinance.module.common.service;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Timestamp;
import java.util.*;

import com.carfinance.module.common.domain.*;
import com.carfinance.module.common.domain.Enum;
import com.carfinance.module.common.vo.MenuVo;
import com.carfinance.module.customermanage.domain.CustomerInfo;
import com.carfinance.module.init.service.InitService;
import com.carfinance.module.login.domain.User;
import com.carfinance.module.vehiclemanage.domain.VehicleInfo;
import com.carfinance.utils.DateTimeUtil;
import com.carfinance.utils.DateUtil;
import net.sf.json.JSONArray;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.carfinance.core.cache.CacheKey;
import com.carfinance.module.common.dao.CommonDao;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Service
public class CommonService {

    static Logger logger = LoggerFactory.getLogger(CommonService.class);

    @Autowired
    private CommonDao commonDao;
    @Autowired
    private ManageMemcacdedClient memcachedClient;
    @Autowired
    private Properties appProps;
    @Autowired
    private InitService initService;

    /**
     * ?????
     * ?
     * ??????????
     * @return
     */
    public Map<String, Object> getUserMenuList(List<UserRole> user_role_list) {
        Map<String, Object> result_map = new HashMap<String, Object>();
        try {
            List<Menu> top_menu_list = new ArrayList<Menu>();//??
            List<Map<String, Object>> return_list = new ArrayList<Map<String, Object>>();//?????json??
            List<Menu> user_all_menu_list = new ArrayList<Menu>();//??

            for (UserRole role : user_role_list) {
                long role_id = role.getRole_id();
                //?role_id???
                List<Menu> role_menu_list = this.getRoleMenuList(role_id);//???????
                user_all_menu_list.addAll(role_menu_list);
            }

            for (Menu menu : user_all_menu_list) {
                Map<String, Object> aaa = new HashMap<String, Object>();
                if (menu.getPid() == 0) {//??????
                    boolean isContainTopMenu = false;
                    long menu_id = menu.getMenu_id();//??id
                    for (Menu top_menu : top_menu_list) {
                        if (top_menu.getMenu_id() == menu_id) {
                            isContainTopMenu = true;
                            break;
                        }
                    }
                    if (!isContainTopMenu) {//????????
                        top_menu_list.add(menu);
                    }

                    List<Menu> sub_menu_list = new ArrayList<Menu>();//???
                    for (Menu sub_menu : user_all_menu_list) {
                        if (sub_menu.getPid() == menu_id) {
                            boolean isContain = false;
                            for (Menu tmp : sub_menu_list) {
                                if (sub_menu.getMenu_id() == tmp.getMenu_id()) {
                                    isContain = true;
                                    break;
                                }
                            }
                            if (!isContain) {//???sub_menu
                                sub_menu_list.add(sub_menu);
                            }
                        }
                    }

                    //                    List<Menu> sub_menu_list = this.commonDao.getMenuListByTopMenuid(menu_id);//???
                    //??????
                    List<MenuVo> sub_menu_vo_list = new ArrayList<MenuVo>();
                    for (Menu sub_menu : sub_menu_list) {
                        MenuVo menu_vo = new MenuVo();
                        menu_vo.setId(sub_menu.getMenu_id());
                        menu_vo.setText(sub_menu.getMenu_name());
                        menu_vo.setHref(appProps.getProperty("base.url") + sub_menu.getMenu_url());
                        sub_menu_vo_list.add(menu_vo);
                    }

                    Map<String, Object> tmp = new HashMap<String, Object>();
                    tmp.put("text", menu.getMenu_name());
                    tmp.put("items", sub_menu_vo_list);

                    List<Map<String, Object>> tmp_list = new ArrayList<Map<String, Object>>();
                    tmp_list.add(tmp);
                    aaa.put("id", menu_id);
                    aaa.put("homePage", menu.getHome_page_id());
                    aaa.put("menu", tmp_list);

                    return_list.add(aaa);
                }

            }
            result_map.put("top_menu_ist", top_menu_list);
            result_map.put("menu_list", new ObjectMapper().writeValueAsString(return_list));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result_map;
    }

    /**
     * ????
     * @param role_id
     * @return
     */
    public List<Menu> getRoleMenuList(long role_id) {
        String key = CacheKey.getRoleMenuListKey(role_id);
        List<Menu> role_menu_list = (List<Menu>) memcachedClient.get(key);
        if (role_menu_list == null) {
            role_menu_list = commonDao.getRoleMenuList(role_id);
            memcachedClient.set(key, 60 * 60, role_menu_list);
        }
        return role_menu_list;
    }

    /**
     * ????
     * @param province_id
     * @return
     */
    public List<City> getProvinceCityList(long province_id) {
        String key = CacheKey.getProvinceCityKey(province_id);
        List<City> city_list = (List<City>) memcachedClient.get(key);
        if (city_list == null) {
            city_list = commonDao.getProvinceCityList(province_id);
            memcachedClient.set(key, 60 * 60, city_list);
        }
        return city_list;
    }

    /**
     * ???
     * @param city_id
     * @return
     */
    public List<Country> getCityCountryList(long city_id) {
        String key = CacheKey.geCityCountryKey(city_id);
        List<Country> country_list = (List<Country>) memcachedClient.get(key);
        if (country_list == null) {
            country_list = commonDao.getCityCountryList(city_id);
            memcachedClient.set(key, 60 * 60, country_list);
        }
        return country_list;
    }

    /**
     * ???
     * @param user_id
     * @return
     */
    public List<UserRole> getUserRoleList(long user_id) {
        String key = CacheKey.getUserRoleListKey(user_id);
        List<UserRole> user_role_list = (List<UserRole>) memcachedClient.get(key);
        if (user_role_list == null) {
            user_role_list = commonDao.getUserRoleList(user_id);
            memcachedClient.set(key, 60 * 60, user_role_list);
        }
        return user_role_list;
    }

    /**
     * ?user_id?
     * @param user_id
     * @return
     */
    public User getUserById(long user_id) {
        return this.commonDao.getUserById(user_id);
    }

    /**
     * ?
     * @param user_id
     * @return
     */
    //    public List<Org> getUserOrgList(long user_id) {
    //        return this.commonDao.getUserOrgList(user_id);
    //    }

    /**
     * ??
     * @param fiel_name
     * @return
     */
    public List<Enum> getEnumFielList(String fiel_name) {
        List<Enum> enum_list = this.initService.getEnum();
        Map<String, List<Enum>> map = new HashMap<String, List<Enum>>();

        for (Enum en : enum_list) {
            List<Enum> tmp_list = new ArrayList<Enum>();
            if (map.get(en.getFiel_name()) != null) {
                tmp_list = map.get(en.getFiel_name());
            }
            tmp_list.add(en);
            map.put(en.getFiel_name(), tmp_list);
        }
        return map.get(fiel_name);
    }

    /**
     * ??
     * ?
     *
     * @return
     */
    public List<City> getSysUsedCityList() {
        String key = CacheKey.getSysUsedCityList();
        List<City> city_list = (List<City>) memcachedClient.get(key);
        if (city_list == null) {
            city_list = commonDao.getSysUsedCityList();
            memcachedClient.set(key, 60 * 60, city_list);
        }
        return city_list;
    }

    /**
     * org_listorgsub_org_list
     * @param org_list
     * @param sub_org_list
     * @return
     */
    private List<Org> getUserSubOrgList(List<Org> org_list, List<Org> sub_org_list) {
        String all_orgid = "";
        for (Org org : org_list) {
            if (org.getOrg_type() < 14) {//?14??
                if ("".equals(all_orgid)) {
                    all_orgid = org.getOrg_id() + "";
                } else {
                    all_orgid = all_orgid + "," + org.getOrg_id();
                }
            }
        }
        //???
        if (!"".equals(all_orgid)) {
            List<Org> sub_tmp = this.commonDao.getUserSubOrgList(all_orgid);
            sub_org_list.addAll(sub_tmp);
            this.getUserSubOrgList(sub_tmp, sub_org_list);
        }
        return sub_org_list;
    }

    /**
     * ???
     * 
     * ????
     * @return
     */
    public List<Org> getUserAllOrgList(long user_id) {
        //?????
        List<Org> org_list = this.commonDao.getUserOrgList(user_id);//?
        List<Org> sub_list = new ArrayList<Org>();
        sub_list = this.getUserSubOrgList(org_list, sub_list);//?

        //???
        List<Org> user_all_org_list = new ArrayList<Org>();
        user_all_org_list.addAll(org_list);
        for (Org sub_org : sub_list) {
            boolean flag = false;//???
            for (Org org : org_list) {
                if (org.getOrg_id() == sub_org.getOrg_id()) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                user_all_org_list.add(sub_org);
            }
        }

        return user_all_org_list;
    }

    /**
     * ???
     * @param user_id
     * @param role_id
     * @return
     */
    public List<Org> getUserRoleOrgList(long user_id, long role_id) {
        return this.commonDao.getUserRoleOrgList(user_id, role_id);
    }

    /**
     * ???
     * @param user_id
     * @return
     */
    public boolean isSysadmin(long user_id) {
        boolean isSysadmin = false;
        List<UserRole> user_role_list = this.getUserRoleList(user_id);
        for (UserRole user_role : user_role_list) {
            if (user_role.getRole_id() == 10000) {
                isSysadmin = true;
                break;
            }
        }
        return isSysadmin;
    }

    //TODO ??????
    private List<CustomerInfo> getAllCustomerInfo() {
        return this.commonDao.getAllCustomerInfo();
    }

    //TODO ??????
    private List<VehicleInfo> getAllVehicles() {
        return this.commonDao.getAllVehicles();
    }

    public List<User> getAllEmployees() {
        return this.commonDao.getAllEmployees();
    }

    public String getAllCustomerName() {
        List<CustomerInfo> customer_info_list = this.getAllCustomerInfo();
        List<String> result = new ArrayList<String>();
        for (CustomerInfo customerInfo : customer_info_list) {
            if (!result.contains(customerInfo.getCustomer_name()))
                result.add(customerInfo.getCustomer_name());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("customer_name_json :" + return_json);
        return return_json;
    }

    public String getAllCustomerDn() {
        List<CustomerInfo> customer_info_list = this.getAllCustomerInfo();
        List<String> result = new ArrayList<String>();
        for (CustomerInfo customerInfo : customer_info_list) {
            if (!result.contains(customerInfo.getCustomer_dn()))
                result.add(customerInfo.getCustomer_dn());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("customer_dn_json :" + return_json);
        return return_json;
    }

    public String getAllCustomerNameAndCertificateNo() {
        List<CustomerInfo> customer_info_list = this.getAllCustomerInfo();
        List<String> result = new ArrayList<String>();
        for (CustomerInfo customerInfo : customer_info_list) {
            result.add(customerInfo.getCustomer_name() + "|" + customerInfo.getCertificate_no());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("customer_name_certification_no_json :" + return_json);
        return return_json;
    }

    public String getAllVehicleBrand() {
        List<VehicleInfo> vehicle_list = this.getAllVehicles();
        List<String> result = new ArrayList<String>();
        for (VehicleInfo vehicleInfo : vehicle_list) {
            if (!result.contains(vehicleInfo.getBrand()))
                result.add(vehicleInfo.getBrand());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("vehicle_brand_json :" + return_json);
        return return_json;
    }

    public String getAllVehicleModel() {
        List<VehicleInfo> vehicle_list = this.getAllVehicles();
        List<String> result = new ArrayList<String>();
        for (VehicleInfo vehicleInfo : vehicle_list) {
            if (!result.contains(vehicleInfo.getModel()))
                result.add(vehicleInfo.getModel());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("vehicle_model_json :" + return_json);
        return return_json;
    }

    public String getAllVehicleLicensePlate() {
        List<VehicleInfo> vehicle_list = this.getAllVehicles();
        List<String> result = new ArrayList<String>();
        for (VehicleInfo vehicleInfo : vehicle_list) {
            if (!result.contains(vehicleInfo.getLicense_plate()))
                result.add(vehicleInfo.getLicense_plate());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("vehicle_license_plate_json :" + return_json);
        return return_json;
    }

    public String getAllEmployeeIdAndName() {
        List<User> user_list = this.getAllEmployees();
        List<String> result = new ArrayList<String>();
        for (User user : user_list) {
            result.add(user.getUser_name() + "|" + user.getEmployee_id());
        }
        String return_json = JSONArray.fromObject(result).toString();
        logger.info("user_employee_id_name_json :" + return_json);
        return return_json;
    }

    public Map<String, Object> saveFile(CommonsMultipartFile upload_file, String save_path) {
        try {
            File f = new File(save_path);
            if (!f.exists()) {
                logger.info("" + save_path);
                f.mkdirs();
            }
            String name = upload_file.getFileItem().getName();
            String extName = "";
            String annexName = "";
            if (name.lastIndexOf(".") >= 0) {
                name = name.substring(name.lastIndexOf("\\") + 1);
                extName = name.substring(name.lastIndexOf("."));
                annexName = name.substring(0, name.lastIndexOf("."));
            }
            if (!"".equals(extName)) {
                name = UUID.randomUUID().toString();
                File file = new File(save_path + name + extName);
                upload_file.getFileItem().write(file);

                String file_name = name + extName;
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("annexName", annexName);
                map.put("file_name", file_name);
                return map;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public List<Org> getSysAllOrgList() {
        return this.commonDao.getSysAllOrgList();
    }

    /**
     * ???????
     * ?????????
     * @param customer_name
     * @param customer_dn
     * @return
     */
    public Map<String, String> getCertificateNoByNameAndDn(String customer_name, String customer_dn) {
        String certificate_type = "";
        String certificate_no = "";
        //??
        List<CustomerInfo> customerInfoList = this.commonDao.getCertificateNoByNameAndDn(customer_name, customer_dn,
                "?");
        if (customerInfoList.size() > 0) {//???
            certificate_type = customerInfoList.get(0).getCertificate_type();
            certificate_no = customerInfoList.get(0).getCertificate_no();
            for (CustomerInfo customerInfo : customerInfoList) {//18????
                if (customerInfo.getCertificate_no().length() == 18) {
                    certificate_type = customerInfo.getCertificate_type();
                    certificate_no = customerInfo.getCertificate_no();
                    break;
                }
            }
        } else {
            customerInfoList = this.commonDao.getCertificateNoByNameAndDn(customer_name, customer_dn, null);
            if (customerInfoList.size() > 0) {
                certificate_type = customerInfoList.get(0).getCertificate_type();
                certificate_no = customerInfoList.get(0).getCertificate_no();
            }
        }

        Map<String, String> map = new HashMap<String, String>();
        map.put("certificate_type", certificate_type);
        map.put("certificate_no", certificate_no);

        return map;
    }

    public String characterFormat(String str, String from_format, String to_format) {
        String return_str = null;
        try {
            if (str != null) {
                return_str = new String(str.getBytes(from_format), to_format);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return return_str;
    }

    public int checkIdCard(String certificate_no) {
        return this.commonDao.checkIdCard(certificate_no);
    }

    public String getContraceNo(long contrace_id) {
        String date = DateUtil.format(new Date(), "yyyyMMdd");
        return "HT" + date + contrace_id;
    }

    /**
     * ????
     * ???????
     * @param report_type
     * @return
     */
    public Map<String, Date> getBeginEndDate(String report_type) {
        String begin_str = null;
        String end_str = null;

        DateTimeUtil dateTimeUtil = new DateTimeUtil();
        if ("day".equals(report_type)) {
            begin_str = DateUtil.format(new Date(), "yyyy-MM-dd") + " 00:00:00";
            end_str = DateUtil.format(new Date(), "yyyy-MM-dd") + " 23:59:59";
        }
        if ("week".equals(report_type)) {
            begin_str = dateTimeUtil.getFirstDayOfWeek("CN") + " 00:00:00";
            end_str = dateTimeUtil.getLastDayOfWeek("CN") + " 23:59:59";
        }
        if ("month".equals(report_type)) {
            begin_str = dateTimeUtil.getMonthFirstDay() + " 00:00:00";
            end_str = dateTimeUtil.getMonthLastDay() + " 23:59:59";
        }
        if ("quarter".equals(report_type)) {
            Calendar c = Calendar.getInstance();
            String year = String.valueOf(dateTimeUtil.getYear());
            int currentMonth = c.get(Calendar.MONTH) + 1;
            try {
                if (currentMonth >= 1 && currentMonth <= 3) {
                    begin_str = year + "-01-01 00:00:00";
                    end_str = year + "-03-31 23:59:59";
                } else if (currentMonth >= 4 && currentMonth <= 6) {
                    begin_str = year + "-04-01 00:00:00";
                    end_str = year + "-06-30 23:59:59";
                } else if (currentMonth >= 7 && currentMonth <= 9) {
                    begin_str = year + "-07-01 00:00:00";
                    end_str = year + "-09-30 23:59:59";
                } else if (currentMonth >= 10 && currentMonth <= 12) {
                    begin_str = year + "-10-01 00:00:00";
                    end_str = year + "-12-31 23:59:59";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("year".equals(report_type)) {
            String year = String.valueOf(dateTimeUtil.getYear());
            begin_str = year + "-01-01 00:00:00";
            end_str = year + "-12-31 23:59:59";
        }

        Date begin = null;
        Date end = null;
        if (begin_str != null && end_str != null) {
            try {
                begin = DateUtil.string2Date(begin_str, "yyyy-MM-dd HH:mm:ss");
                end = DateUtil.string2Date(end_str, "yyyy-MM-dd HH:mm:ss");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        Map<String, Date> map = new HashMap<String, Date>();
        map.put("begin", begin);
        map.put("end", end);

        return map;
    }
}