package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StaffSalary;
import com.ssm.manage.query.StaffSalaryRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStaffSalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value="员工工资controller",tags={"员工工资相关接口"})
@RestController
@RequestMapping("/demay/ssm/salary")
@Slf4j
public class SsmStaffSalaryController {
    @Autowired
    private SsmStaffSalaryService ssmStaffSalaryService;

    @ApiOperation(value = "查询员工工资  门店端-统计报表-查询员工工资", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectSalary")
    public GenericResponse selectSalary(@RequestBody StaffSalaryRequest staffSalaryRequest, HttpSession session) {
        GenericResponse<StaffSalary> genericResponse = new GenericResponse();
        List<StaffSalary> list = new ArrayList<>();
        if(null ==staffSalaryRequest.getStoreId() || "".equals(staffSalaryRequest.getStoreId())){
            staffSalaryRequest.setStoreId(SessionHelp.getStoreId(session));
            staffSalaryRequest.setStoreName(SessionHelp.getStoreName(session));
        }
        try {
            list = ssmStaffSalaryService.selectSalary(staffSalaryRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffSalaryController.selectSalary:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询员工工资  门店端-统计报表-查询员工工资(升级后)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectSalarys")
    public GenericResponse selectSalarys(@RequestBody StaffSalaryRequest staffSalaryRequest, HttpSession session) {
        GenericResponse<StaffSalary> genericResponse = new GenericResponse();
        List<StaffSalary> list = new ArrayList<>();
        if(null ==staffSalaryRequest.getStoreId() || "".equals(staffSalaryRequest.getStoreId())){
            staffSalaryRequest.setStoreId(SessionHelp.getStoreId(session));
            staffSalaryRequest.setStoreName(SessionHelp.getStoreName(session));
        }
        try {
            list = ssmStaffSalaryService.selectSalarys(staffSalaryRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffSalaryController.selectSalary:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "添加员工工资  门店端-统计报表-添加员工工资", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse addSalary(@RequestBody List<StaffSalaryRequest> staffSalaryRequests) {
        GenericResponse<StaffSalary> genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmStaffSalaryService.add(staffSalaryRequests);
        } catch (Exception e) {
            log.error("SsmStaffSalaryController.addSalary:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "删除员工工资  门店端-统计报表-删除员工工资", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/delete")
    public GenericResponse deleteSalary(@RequestBody StaffSalaryRequest staffSalaryRequest, HttpSession session) {
        GenericResponse<StaffSalary> genericResponse = new GenericResponse();
        staffSalaryRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmStaffSalaryService.delete(staffSalaryRequest);
        } catch (Exception e) {
            log.error("SsmStaffSalaryController.deleteSalary:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


}
