package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.RepayListRequest;
import com.ssm.manage.query.RepayTradeRequest;
import com.ssm.manage.response.RepayListResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmRepayService;
import com.ssm.manage.service.SsmRepayTradeService;
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={"还款相关接口"})
@RestController
@RequestMapping("/demay/ssm/repay")
@Slf4j
public class SsmRepayController {
    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;



    @ApiOperation(value = "还款单添加  (门店端-还款单-还款单添加)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody RepayListRequest repayListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        repayListRequest.setStoreId(storeId);
//        repayListRequest.setStoreName(storeName);
//        repayListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmRepayService.addRepay(repayListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRepayController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "还款单作废  (门店端-还款单-还款单作废)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cancel")
    public GenericResponse cancel(@RequestBody RepayListRequest repayListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        repayListRequest.setStoreId(storeId);
//        repayListRequest.setStoreName(storeName);
//        repayListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmRepayService.cancel(repayListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRepayController.cancel:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "还款单修改业务员  (门店端-还款单-还款单修改业务员)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateStaff")
    public GenericResponse updateStaff(@RequestBody RepayTradeRequest repayTradeRequest,HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
        repayTradeRequest.setStoreId(storeId);
//        repayTradeRequest.setStoreName(storeName);
        int i = 0;
        try {
            i = ssmRepayTradeService.updateStaff(repayTradeRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRepayController.updateStaff:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
