package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.utils.ChuangLanBeanSmsUtil;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.CourseListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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 java.io.IOException;

@Api(value="app疗程controller",tags={"app疗程相关接口"})
@RestController
@RequestMapping("/demay/ssm/course")
@Slf4j
public class SsmCourseController {
    @Autowired
    private SsmCourseService ssmCourseService;
    @Autowired
    private ChuangLanBeanSmsUtil chuangLanBeanSmsUtil;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;


    @ApiOperation(value = "疗程单添加  (门店端-疗程单-疗程单添加)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody CourseListRequest courseListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        courseListRequest.setStoreId(storeId);
//        courseListRequest.setStoreName(storeName);
//        courseListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmCourseService.addCourse(courseListRequest);
        }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);
            //发送短信
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(courseListRequest.getStoreId());
            //设置短信账户 发送 否则不发送
            if (StringUtils.isNotBlank(baseStore.getSmsAccount()) && null != baseStore.getMessagingPlatform()) {
                try {
//                    chuangLanBeanSmsUtil.sendSLCardUseSms(baseStore, courseListRequest.getMemberName(), courseListRequest.getMobilePhone(), courseListRequest.getProjectName(), courseListRequest.getNumberTimes());
                    chuangLanBeanSmsUtil.sendCourseSms(baseStore, courseListRequest.getMobilePhone(), courseListRequest.getProjectName());
                    genericResponse.setCodeAndMsg(ResponseCode.OK);
                } catch (IOException e) {
                    genericResponse.setCodeAndMsg(ResponseCode.SMS_SEND_OK_EXCEPTION);
                    log.error("SsmCourseController.add:", e);
                } catch (SmsException e) {
                    log.error("SsmCourseController.add:", e);
                }
            }else {
                genericResponse.setCodeAndMsg(ResponseCode.SMS_NO_OK);
            }
        }
        return genericResponse;
    }

    @ApiOperation(value = "疗程单作废  (门店端-疗程单-疗程单作废)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cancel")
    public GenericResponse cancel(@RequestBody CourseListRequest courseListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        courseListRequest.setStoreId(storeId);
//        courseListRequest.setStoreName(storeName);
//        courseListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmCourseService.cancel(courseListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCourseController.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 CourseListRequest courseListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        courseListRequest.setStoreId(storeId);
//        courseListRequest.setStoreName(storeName);
//        courseListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmCourseService.updateStaff(courseListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCourseController.updateStaff:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
