kitt.site.controller.CompanyController.java Source code

Java tutorial

Introduction

Here is the source code for kitt.site.controller.CompanyController.java

Source

package kitt.site.controller;

import kitt.core.domain.Company;
import kitt.core.domain.CompanyVerify;
import kitt.core.domain.District;
import kitt.core.domain.User;
import kitt.core.persistence.*;
import kitt.core.service.FileStore;
import kitt.core.service.MessageNotice;
import kitt.core.util.check.CheckPictureInfo;
import kitt.site.basic.JsonController;
import kitt.site.basic.annotation.CurrentUser;
import kitt.site.basic.annotation.LoginRequired;
import kitt.site.basic.exception.BusinessException;
import kitt.site.service.Auth;
import kitt.site.service.BeanValidators;
import kitt.site.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanjun on 14-11-12.
 */
@LoginRequired
@Controller
public class CompanyController extends JsonController {
    @Autowired
    protected CompanyMapper companyMapper;
    @Autowired
    protected BuyMapper buyMapper;
    @Autowired
    protected Auth auth;
    @Autowired
    protected UserMapper userMapper;
    @Autowired
    protected OrderMapper orderMapper;
    @Autowired
    protected PaymentMapper paymentMapper;
    @Autowired
    protected FileService fileService;
    @Autowired
    protected FileStore fileStore;
    @Autowired
    private CheckPictureInfo checkPictureInfo;
    @Autowired
    private RegionYMMapper regionMapper;

    //???(???????,??)
    @RequestMapping(value = "/account/saveCompany", method = RequestMethod.POST)
    @ResponseBody
    public Object saveCompany(Company company, @CurrentUser User user) throws Exception {
        BeanValidators.validateWithException(company);
        boolean result = false;
        if (!"".equals(userMapper.getUserById(session.getUser().getId()).getVerifystatus())) {
            //temp,?upload
            fileStore.copyFileToUploadDir(company.getBusinesslicense());
            fileStore.copyFileToUploadDir(company.getIdentificationnumber());
            fileStore.copyFileToUploadDir(company.getOrganizationcode());
            fileStore.copyFileToUploadDir(company.getOpeninglicense());
            if (!StringUtils.isBlank(company.getOperatinglicense())) {
                fileStore.copyFileToUploadDir(company.getOperatinglicense());
                company.setOperatinglicense(company.getOperatinglicense().replace("temp", "upload"));
            }

            fileStore.copyFileToUploadDir(company.getOpeninglicense());
            if (!StringUtils.isBlank(company.getInvoicinginformation())) {
                fileStore.copyFileToUploadDir(company.getInvoicinginformation());
                company.setInvoicinginformation(company.getInvoicinginformation().replace("temp", "upload"));
            }

            company.setBusinesslicense(company.getBusinesslicense().replace("temp", "upload"));
            company.setIdentificationnumber(company.getIdentificationnumber().replace("temp", "upload"));
            company.setOrganizationcode(company.getOrganizationcode().replace("temp", "upload"));
            company.setOpeninglicense(company.getOpeninglicense().replace("temp", "upload"));
            company.setUserid(session.getUser().getId());

            if (companyMapper.countCompany(session.getUser().getId()) == 0) {
                companyMapper.addCompany(company);
            } else {
                companyMapper.modifyCompany(company);
            }
            companyMapper.addCompVerify(new CompanyVerify("", LocalDateTime.now(),
                    companyMapper.getIdByUserid(session.getUser().getId()), session.getUser().getId()));
            companyMapper.setCompanyStatus("", null,
                    companyMapper.getIdByUserid(session.getUser().getId()));
            userMapper.setUserVerifyStatus("", null, session.getUser().getId());
            result = true;
            MessageNotice.SubmitCompany.noticeUser(user.getSecurephone());
        }
        return result;
    }

    //??
    @RequestMapping("/account/getAllProvinces")
    @ResponseBody
    public Object getAllProvinces() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<District> moldPList = regionMapper.getDistinctMold(1, null);
        if (moldPList != null && moldPList.size() > 0) {
            for (District d : moldPList) {
                d.setRegionList(regionMapper.getregionymsByMold(d.getMold(), null, 1));
            }
        }
        map.put("provinceList", moldPList);
        return map;
    }

    //?code???
    @RequestMapping("/account/getCitysByParent")
    @ResponseBody
    public Object getCitysByParent(@RequestParam(value = "code", required = true) String code) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<District> moldPList = regionMapper.getDistinctMold(2, code);
        if (moldPList != null && moldPList.size() > 0) {
            for (District d : moldPList) {
                d.setRegionList(regionMapper.getregionymsByMold(d.getMold(), code, 2));
            }
        }
        map.put("cityList", moldPList);
        return map;
    }

    //?code??
    @RequestMapping("/account/getCountrysByParent")
    @ResponseBody
    public Object getCountrysByParent(@RequestParam(value = "code", required = true) String code) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<District> moldPList = regionMapper.getDistinctMold(3, code);
        if (moldPList != null && moldPList.size() > 0) {
            for (District d : moldPList) {
                d.setRegionList(regionMapper.getregionymsByMold(d.getMold(), code, 3));
            }
        }
        map.put("countryList", moldPList);
        return map;
    }

    //??
    @RequestMapping(value = "/account/getCompany", method = RequestMethod.POST)
    @ResponseBody
    public Company getCompany() {
        return companyMapper.getCompanyByUserid(session.getUser().getId());
    }

    //??
    @RequestMapping(value = "/account/saveCompanyPic", method = RequestMethod.POST)
    @ResponseBody
    public Object saveCompanyPic(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!checkPictureInfo.doCheckPictureType(file)) {
            throw new BusinessException(" .jpg, .bmp, .png, .jpeg ??");
        } else if (!checkPictureInfo.doCheckPictureSize(file)) {
            throw new BusinessException("?10M?");
        } else {
            map.put("filePath", fileService.uploadPicture(file));
            map.put("success", true);
            return map;
        }
    }

    //?????
    @RequestMapping(value = "/account/checkCompanyname", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkCompanyname(@RequestParam("name") String name) {
        int i = companyMapper.countCompanyIsExist(name.trim(), session.getUser().getId());
        if (i == 0) {
            return true;
        } else {
            return false;
        }
    }
}