MobileUserController.java 6.41 KB
package com.huaheng.mobile.general;

import com.alibaba.fastjson.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.receipt.reservation.domain.Reservation;
import com.huaheng.pc.receipt.reservation.service.ReservationService;
import com.huaheng.pc.system.menu.domain.Menu;
import com.huaheng.pc.system.menu.service.IMenuService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author Enzo Cotter
 * @date 2019/12/15
 */
@RestController
@RequestMapping("/mobile/")
@Api(tags = {"移动端用户信息"}, value = "移动端用户信息MobileUserController")
public class MobileUserController extends BaseController {

    @Resource
    private IMenuService menuService;
    @Resource
    private IUserService userService;
    @Resource
    private CompanyService companyService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private ReservationService reservationService;

    @PostMapping("/login")
    @ApiOperation("用户登陆")
    public AjaxResult login(@RequestBody @ApiParam(value="code和password的Map集合") Map<String, String> param) {
        if  (param.get("code") == null) {
            throw new JSONException("code(用户名)不能为空");
        }
        if  (param.get("password") == null) {
            throw new JSONException("password(密码)不能为空");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(param.get("code"), param.get("password"), false);
        Subject subject = SecurityUtils.getSubject();
        SecurityUtils.getSubject().getSession().setTimeout(-1000L);
        try {
            subject.login(token);
            List<Map<String, Object>> list = userService.getWarehouseByUserCode(param.get("code"));
            return AjaxResult.success(list);
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @PostMapping("/getModules")
    @ApiOperation("获取当前用户模块列表")
    public AjaxResult  getModules(@RequestBody @ApiParam(value="WarehouseId和warehouseCode的Map集合") Map<String, String> param) {
        if  (param.get("warehouseCode") == null) {
            throw new JSONException("warehouseCode(仓库编码)不能为空");
        }
        User user = ShiroUtils.getUser();
        user.setWarehouseCode(param.get("warehouseCode"));
        ShiroUtils.setUser(user);
        List<Company> companys = companyService.selectCompanyByCurrentUserId();
        user.setCompanyIdList(companys.stream().map(X -> X.getId()).collect(Collectors.toList()));
        user.setCompanyCodeList(companys.stream().map(X -> X.getCode()).collect(Collectors.toList()));
        ShiroUtils.setUser(user);
        List<Menu> menus = menuService.selectMobileMenusByUserId(ShiroUtils.getUserId());
        return AjaxResult.success(menus);
    }

    @PostMapping("/heartbeat")
    @ApiOperation("心跳接口,用于延长cookie有效期")
    public AjaxResult heartbeat()
    {
        return AjaxResult.success("success");
    }

    @PostMapping("/getCompanyInfo")
    @ApiOperation("获取公司信息")
    public AjaxResult getCompanyInfo() {
        List<Company> companies = companyService.selectCompanyByCurrentUserId();
        List<CompanyInfo> companyInfos = new ArrayList<>();
        for(Company company : companies) {
            companyInfos.add(new CompanyInfo(company.getId(), company.getCode(), company.getName()));
        }
        return AjaxResult.success(companyInfos);
    }

    @PostMapping("/getWarehouseList")
    @ApiOperation("获取公司信息")
    public AjaxResult getWarehouseList() {
        Warehouse warehouse = new Warehouse();
        List<Warehouse> warehouseList = warehouseService.selectListEntityByEqual(warehouse);
        return AjaxResult.success(warehouseList);
    }

    @PostMapping("/reservation")
    public AjaxResult reservation(String localDate, String warehouseCode) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date2 = LocalDate.parse(localDate, fmt);
        return AjaxResult.success(reservationService.reservationTime(date2, warehouseCode));
    }

    /**
     * 新增保存预约
     */
    @ApiOperation(value = "新增预约 ", notes = "新增预约 ", httpMethod = "POST")
    @Log(title = "预约 ", operating = "新增预约 ", action = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(@RequestBody @ApiParam(value="WarehouseId和warehouseCode的Map集合") Reservation reservation) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        reservation.setBeginTime(LocalDateTime.parse(reservation.getUserDef1()));
        reservation.setEndTime(LocalDateTime.parse(reservation.getUserDef2()));
        reservation.setUserDef1(null);
        reservation.setUserDef2(null);
        return toAjax(reservationService.save(reservation));
    }
}