package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.CancelDebtList;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.CancelDebtListRequest;
import com.ssm.manage.response.CancelDebtListReponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCancelDebtListService;
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;

@Api(value="会员欠款取消controller(新)",tags={"门店会员欠款取消controller相关接口(新)"})
@RestController
@RequestMapping("/demay/ssm/CancelDebtList")
@Slf4j
public class CancelDebtListController {

    @Autowired
    private SsmCancelDebtListService ssmCancelDebtListService;

    @ApiOperation(value = "查询取消欠款单",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody CancelDebtListRequest cashListRequest, HttpSession session){
        PageResponse<CancelDebtListReponse> pageResponse = new PageResponse<>();
        PageInfo<CancelDebtListReponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(storeId==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        cashListRequest.setStoreId(storeId);
        try {
            pageInfo = ssmCancelDebtListService.selectPage(cashListRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "取消欠款单添加(此功能不再修改)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody CancelDebtListRequest cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        cancelDebtListRequest.setStoreId(storeId);
        cancelDebtListRequest.setStoreName(storeName);
        cancelDebtListRequest.setSession(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.add(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody CancelDebtListRequest cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.update(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/submit")
    public GenericResponse submit(@RequestBody CancelDebtList cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.submit(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/recycle")
    public GenericResponse recycle(@RequestBody CancelDebtList cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.recycle(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/audit")
    public GenericResponse audit(@RequestBody CancelDebtList cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.audit(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/theaudit")
    public GenericResponse theaudit(@RequestBody CancelDebtList cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.theaudit(cancelDebtListRequest,staffPhone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody CancelDebtList cancelDebtListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        if(storeId==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmCancelDebtListService.delete(cancelDebtListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e){
            log.error("SsmCourseController.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)
    @RequestMapping("/selectById")
    public PageResponse selectById(@RequestBody CancelDebtListRequest cashListRequest, HttpSession session){
        PageResponse<CancelDebtListReponse> pageResponse = new PageResponse<>();
        PageInfo<CancelDebtListReponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(storeId==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        cashListRequest.setStoreId(storeId);
        try {
            pageInfo = ssmCancelDebtListService.selectById(cashListRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

}
