package com.ssm.controller;


import javax.persistence.Column;
import javax.persistence.Id;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.entity.RlStaff;
import com.ssm.manage.dto.RlStaffDto;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.RlStaffVo;
import com.ssm.manage.query.StaffRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.RlStaffService;
import com.ssm.manage.utils.SessionHelp;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import java.io.Serializable;

import lombok.extern.slf4j.Slf4j;
import com.ssm.core.utils.SnowflakeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 门店员工档案(RlStaff)表控制层
 *
 * @author makejava
 * @since 2023-12-13 15:34:07
 */
@RestController
@RequestMapping("rlStaff")
@Slf4j
@Api(tags = "门店员工档案")
public class RlStaffController {
    /**
     * 服务对象
     */
    @Autowired
    private RlStaffService rlStaffService;

/**
 * 通过主键查询单条数据
 *
 * @param id 主键
 * @return
 @GetMapping("{id}")
 @ApiOperation("通过主键查询单条数据")
 //@RequiresPermissions("system:rlStaff:query")
 public ResultVo get(@PathVariable Serializable id) {
 return this.rlStaffService.queryById(id);
 }
 */


    /**
     * 分页查询门店员工档案
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询门店员工档案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//@RequiresPermissions("system:rlStaff:query")
    public PageResponse<RlStaff> listPage(RlStaffDto dto) {
        PageResponse<RlStaff> pageResponse = new PageResponse<>();
        PageInfo<RlStaff> pageInfo = new PageInfo<>();
        try {
            pageInfo = this.rlStaffService.selectPage(dto);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 新增门店员工档案
     *
     * @param rlStaff
     * @return
     */
    @PostMapping
    @ApiOperation("新增门店员工档案")
//@RequiresPermissions("system:rlStaff:add")
    public GenericResponse add(@RequestBody RlStaff rlStaff) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            rlStaff.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            i = this.rlStaffService.save(rlStaff);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 编辑门店员工档案
     *
     * @param rlStaff
     * @return
     */
    @PutMapping
    @ApiOperation("编辑门店员工档案")
//@RequiresPermissions("system:rlStaff:edit")
    public GenericResponse edit(@RequestBody RlStaff rlStaff) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = this.rlStaffService.updateByPrimaryKey(rlStaff);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 根据主键删除门店员工档案
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除门店员工档案")
//@RequiresPermissions("system:rlStaff:delete")
    public GenericResponse delete(@PathVariable Long id) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            RlStaff rlStaff = new RlStaff();
            rlStaff.setId(id);
            i = this.rlStaffService.delete(rlStaff);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "门店员工密码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLogin", method= RequestMethod.POST)
    public GenericResponse<RlStaffVo> phoneLogin(@RequestBody RlStaffDto rlStaffDto, HttpSession session) {
        GenericResponse<RlStaffVo> genericResponse = new GenericResponse<>();
        RlStaffVo staffResponse = null;
        if (StringUtils.isBlank(rlStaffDto.getMobilePhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        if (StringUtils.isBlank(rlStaffDto.getPassword())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"密码");
            return genericResponse;
        }
        try{
            staffResponse = rlStaffService.phoneLogin(rlStaffDto);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (staffResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(staffResponse);
        SessionHelp.setToken(session,staffResponse.getToken());
        SessionHelp.setLoginAccount(session,rlStaffDto.getMobilePhone());
        SessionHelp.setStoreId(session,staffResponse.getStoreId());
        SessionHelp.setStoreName(session,rlStaffDto.getStoreName());
        session.setMaxInactiveInterval(MAX_INACTIVE_INTERVAL);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    public static final int MAX_INACTIVE_INTERVAL = 60 * 60 * 24;
}
