package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.BaseWorkType;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.BaseWorkTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseWorkTypeService;
import com.ssm.util.RedisClient;
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/BaseWorkType")
@Slf4j
public class SsmBaseWorkTypeController {

    @Autowired
    private SsmBaseWorkTypeService ssmBaseWorkTypeService;

    @Autowired
    public RedisClient redisClient;

    @ApiOperation(value = "查询工种",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody BaseWorkTypeRequest baseWorkTypeRequest, HttpSession session){
        PageResponse<BaseWorkType> pageResponse = new PageResponse<>();
        List<BaseWorkType> pageInfo = new ArrayList<>();
        Long storeId = (long)redisClient.get("StoreId");
        // storeId = SessionHelp.getStoreId(session);
        baseWorkTypeRequest.setStoreId(storeId);
        try {
            pageInfo = ssmBaseWorkTypeService.selectPage(baseWorkTypeRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setData(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "添加工种(只能总店操作)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody BaseWorkTypeRequest baseWorkTypeRequest, HttpSession session) {
        GenericResponse<BaseWorkType> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        baseWorkTypeRequest.setStoreName(storeName);
        baseWorkTypeRequest.setStoreId(storeId);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBaseWorkTypeService.add(baseWorkTypeRequest,phone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加工种(只能总店操作)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody BaseWorkTypeRequest baseWorkTypeRequest, HttpSession session) {
        GenericResponse<BaseWorkType> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBaseWorkTypeService.update(baseWorkTypeRequest,phone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "启用和停用工种(只能总店操作)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/updateState")
    public GenericResponse updateState(@RequestBody BaseWorkTypeRequest baseWorkTypeRequest, HttpSession session) {
        GenericResponse<BaseWorkType> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBaseWorkTypeService.updateState(baseWorkTypeRequest,phone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "删除工种(只能总店操作)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody BaseWorkTypeRequest baseWorkTypeRequest, HttpSession session) {
        GenericResponse<BaseWorkType> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBaseWorkTypeService.delete(baseWorkTypeRequest);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
