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.SmsException;
import com.ssm.manage.query.CoursePayListRequest;
import com.ssm.manage.query.RechargeTurnCourseRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmRechargeTurnCourseService;
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/rechargeTurnCourse")
@Slf4j
public class SsmRechargeTurnCourseController {

    @Autowired
    private SsmRechargeTurnCourseService ssmRechargeTurnCourseService;

    @ApiOperation(value = "查询疗程转充值列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session){
        PageResponse<RechargeTurnCourseRequest> pageResponse = new PageResponse<>();
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        rechargeTurnCourseRequest.setStoreId(storeId);
        try {
            pageInfo = ssmRechargeTurnCourseService.selectPage(rechargeTurnCourseRequest);
        }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("/pageNew")
    public PageResponse selectPageNew(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session){
        PageResponse<RechargeTurnCourseRequest> pageResponse = new PageResponse<>();
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        List<Long> listStoreId=new ArrayList<>();
        if(null == rechargeTurnCourseRequest.getStoreList() || rechargeTurnCourseRequest.getStoreList().size() == 0){
            //自动查询
            rechargeTurnCourseRequest.setSelectType(1);
            rechargeTurnCourseRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            rechargeTurnCourseRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            rechargeTurnCourseRequest.setSelectType(0);
        }
        try {
            pageInfo = ssmRechargeTurnCourseService.selectPageNew(rechargeTurnCourseRequest);
        }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("/pageDetail")
    public PageResponse pageDetail(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session){
        PageResponse<RechargeTurnCourseRequest> pageResponse = new PageResponse<>();
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmRechargeTurnCourseService.pageDetail(rechargeTurnCourseRequest);
        }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, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
//        rechargeTurnCourseRequest.setStoreName(storeName);
//        rechargeTurnCourseRequest.setStoreId(storeId);
        if(null == rechargeTurnCourseRequest.getRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转入的充值卡账户");
            return genericResponse;
        }
        /*if(null == rechargeTurnCourseRequest.getCoursePayLists()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转出的疗程账户");
            return genericResponse;
        }*/
        if(null == rechargeTurnCourseRequest.getCoursePayListRequests()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转出的疗程账户");
            return genericResponse;
        }
        int i = 0;
        try {
            for(CoursePayListRequest coursePayListRequest:rechargeTurnCourseRequest.getCoursePayListRequests()){
                if(coursePayListRequest.getRollOffCount().longValue()<0||rechargeTurnCourseRequest.getRechargeInMoney().longValue()<0||coursePayListRequest.getRollOffMoney().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            i = ssmRechargeTurnCourseService.add(rechargeTurnCourseRequest,staffPhone);
        } 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 RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.update(rechargeTurnCourseRequest,staffPhone);
        } 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("/submit")
    public GenericResponse submit(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.submit(rechargeTurnCourseRequest,staffPhone);
        } 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("/recycle")
    public GenericResponse recycle(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.recycle(rechargeTurnCourseRequest,staffPhone);
        } 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("/audit")
    public GenericResponse audit(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.audit(rechargeTurnCourseRequest,staffPhone);
        } 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("/theAudit")
    public GenericResponse theAudit(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.theAudit(rechargeTurnCourseRequest,staffPhone);
        } 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("/cancel")
    public GenericResponse cancel(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.cancel(rechargeTurnCourseRequest,staffPhone);
        } 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 RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeTurnCourseService.delete(rechargeTurnCourseRequest);
        } 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;
    }
}
