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.core.utils.ChuangLanBeanSmsUtil;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.ServiceOrdersRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.ServiceOrdersResponse;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmServiceOrdersService;
import com.ssm.manage.utils.Constant;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api(value="房间技师服务明细表controller",tags={"房间技师服务明细表相关接口"})
@RestController
@RequestMapping("/demay/ssm/serviceOrders")
@Slf4j
public class SsmServiceOrdersController {

    @Autowired
    private ChuangLanBeanSmsUtil chuangLanBeanSmsUtil;
    @Autowired
    private SsmServiceOrdersService ssmServiceOrdersService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "查询历史单据（按月）",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody ServiceOrdersRequest serviceOrdersRequest, HttpSession session){
        PageResponse<ServiceOrdersResponse> pageResponse = new PageResponse<>();
        PageInfo<ServiceOrdersResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        serviceOrdersRequest.setStoreId(storeId);
        try {
            pageInfo = ssmServiceOrdersService.selectPage(serviceOrdersRequest);
        }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("/pages")
//    public PageResponse selectPages(@RequestBody ServiceOrdersRequest serviceOrdersRequest, HttpSession session){
//        PageResponse<ServiceOrdersResponse> pageResponse = new PageResponse<>();
//        PageInfo<ServiceOrdersResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        serviceOrdersRequest.setStoreId(storeId);
//        try {
//            pageInfo = ssmServiceOrdersService.selectPages(serviceOrdersRequest);
//        }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 List<ServiceOrdersRequest> serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        for (ServiceOrdersRequest serviceOrdersRequest : serviceOrderRequest) {
            serviceOrdersRequest.setStoreName(storeName);
            serviceOrdersRequest.setStoreId(storeId);
        }

        String phone = SessionHelp.getLoginAccount(session);
        List<AccountTradeRecord> list = new ArrayList<>();
        try {
            list = ssmServiceOrdersService.add(serviceOrderRequest,phone);
        } 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 (list.size() > 0){
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
            for (AccountTradeRecord accountTradeRecord : list) {
                //发送短信 充值卡支付
                if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())) {
                    //设置短信账户 发送 否则不发送
                    if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
                        try {
                            chuangLanBeanSmsUtil.sendCashByRechargeSms(baseStore, accountTradeRecord.getMemberPhone(), accountTradeRecord.getAccountSpendMoney(), accountTradeRecord.getAccountConsumeMoney());
                        } catch (IOException e) {
                            log.error("SsmCashController.doCash:", e);
                        } catch (SmsException e) {
                            log.error("SsmCashController.doCash:", e);
                            genericResponse.setCodeAndMsg(e);
                        }
                    }
                } else if (Constant.account_type_project_consume.equals(accountTradeRecord.getAccountType())) {//疗程卡支付
                    //设置短信账户 发送 否则不发送
                    if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
                        try {
//                            chuangLanBeanSmsUtil.sendSLStoreCashProjectPaySms(baseStore, accountTradeRecord.getMemberName(), accountTradeRecord.getMemberPhone(), accountTradeRecord.getTradeName(), accountTradeRecord.getAccountSpendCount(), accountTradeRecord.getAccountConsumeCount());
                            chuangLanBeanSmsUtil.sendCashByCourseSms(baseStore, accountTradeRecord.getMemberPhone(), accountTradeRecord.getTradeName(), accountTradeRecord.getAccountSpendCount(), accountTradeRecord.getAccountConsumeCount());
                        } catch (IOException e) {
                            log.error("SsmCashController.doCash:", e);
                        } catch (SmsException e) {
                            log.error("SsmCashController.doCash:", e);
                            genericResponse.setCodeAndMsg(e);
                        }
                    }
                }
            }

        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "修改业务员", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody List<ServiceOrdersRequest> serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        for (ServiceOrdersRequest serviceOrdersRequest : serviceOrderRequest) {
            serviceOrdersRequest.setStoreName(storeName);
            serviceOrdersRequest.setStoreId(storeId);
        }

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmServiceOrdersService.update(serviceOrderRequest,phone);
        } 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 ServiceOrdersRequest serviceOrderRequests, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();
        String phone = SessionHelp.getLoginAccount(session);
        Long storeId = SessionHelp.getStoreId(session);
        int i = 0;
        try {
            i = ssmServiceOrdersService.cancel(serviceOrderRequests,phone,storeId);
        } 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;
    }

}
