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.DocPromotionRequest;
import com.ssm.manage.response.DocPromotionResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmDocPromotionService;
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/docPromotion")
@Slf4j
public class SsmDocPromotionController {
    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;


    @ApiOperation(value = "促销活动单添加  (app端-促销活动单-促销活动单添加)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody DocPromotionRequest docPromotionRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        docPromotionRequest.setStoreId(storeId);
//        docPromotionRequest.setStoreName(storeName);
//        docPromotionRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmDocPromotionService.addDoc(docPromotionRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmDocPromotionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


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

    @ApiOperation(value = "促销活动单业务员修改  (app端-促销活动单-促销活动单业务员修改)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/updateStaff")
    public GenericResponse updateStaff(@RequestBody DocPromotionRequest docPromotionRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        docPromotionRequest.setStoreId(storeId);
//        docPromotionRequest.setStoreName(storeName);
//        docPromotionRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmDocPromotionService.updateStaff(docPromotionRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmDocPromotionController.updateStaff:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
