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.utils.ChuangLanBeanSmsUtil;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.CardChangeList;
import com.ssm.manage.entity.CardChangeOutProject;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.CardChangeListRequest;
import com.ssm.manage.query.CardChangeOutProjectRequest;
import com.ssm.manage.response.CardChangeInProjectResponse;
import com.ssm.manage.response.CardChangeOutProjectResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmCardChangeInProjectService;
import com.ssm.manage.service.SsmCardChangeOutProjectService;
import com.ssm.manage.service.SsmCardChangeService;
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;
import java.util.ArrayList;
import java.util.List;

@Api(value = "加价Controller",tags = {"加价相关接口"})
@RestController
@RequestMapping("/demay/ssm/cardChange")
@Slf4j
public class SsmCardChangeController {
    @Autowired
    private SsmCardChangeService ssmCardChangeService;
    @Autowired
    private ChuangLanBeanSmsUtil chuangLanBeanSmsUtil;

    @Autowired
    private SsmCardChangeInProjectService ssmCardChangeInProjectService;

    @Autowired
    private SsmCardChangeOutProjectService ssmCardChangeOutProjectService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "加价转疗程分页列表查询 门店端-加价-加价转疗程分页列表查询",httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody CardChangeListRequest cardChangeListRequest) {
        PageResponse<CardChangeList> pageResponse = new PageResponse<>();
        PageInfo<CardChangeList> pageInfo = new PageInfo<>();
//        cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmCardChangeService.selectPage(cardChangeListRequest);
        } catch (Exception e) {
            log.error("SsmCardChangeController.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("/inPro")
    public GenericResponse inPro(@RequestBody CardChangeListRequest cardChangeListRequest){
        GenericResponse<CardChangeInProjectResponse> genericResponse = new GenericResponse<>();
        List<CardChangeInProjectResponse> list = new ArrayList<>();
//        cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            list = ssmCardChangeInProjectService.selectList(cardChangeListRequest);
        } catch (Exception e) {
            log.error("SsmCardChangeController.inPro", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "加价转疗程转出疗程明细查询 门店端-加价-加价转疗程转出疗程明细查询",httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/outPro")
    public GenericResponse outPro(@RequestBody CardChangeOutProjectRequest cardChangeOutProjectRequest){
        /*GenericResponse<CardChangeOutProject> genericResponse = new GenericResponse<>();
        List<CardChangeOutProject> list = new ArrayList<>();

//        cardChangeOutProjectRequest.setStoreId(SessionHelp.getStoreId(session));

        try {
            list = ssmCardChangeOutProjectService.selectList(cardChangeOutProjectRequest);
        } catch (Exception e) {
            log.error("SsmCardChangeController.outPro", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;*/

        GenericResponse<CardChangeOutProjectResponse> genericResponse = new GenericResponse<>();
        List<CardChangeOutProjectResponse> list = new ArrayList<>();
        //cardChangeOutProjectRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            list = ssmCardChangeOutProjectService.selectList1(cardChangeOutProjectRequest);
        } catch (Exception e) {
            log.error("SsmCardChangeController.outPro", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "加价转疗程单据添加 门店端-加价-加价转疗程单据添加",httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody CardChangeListRequest cardChangeListRequest){
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
//        cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        cardChangeListRequest.setStoreName(SessionHelp.getStoreName(session));
//        cardChangeListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        try {
            if(cardChangeListRequest.getChangeOutMoney().longValue()<0||cardChangeListRequest.getChangeInPayMoney().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmCardChangeService.add(cardChangeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCardChangeController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            //genericResponse.setCodeAndMsg(ResponseCode.OK);
            //发送短信
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(cardChangeListRequest.getStoreId());
            //设置短信账户 发送 否则不发送
            if (StringUtils.isNotBlank(baseStore.getSmsAccount()) && null != baseStore.getMessagingPlatform()) {
                try {
                    chuangLanBeanSmsUtil.sendCourseSms(baseStore, cardChangeListRequest.getMobilePhone(), cardChangeListRequest.getCardChangeInProjectRequest().getProjectName());
                    genericResponse.setCodeAndMsg(ResponseCode.OK);
                } catch (IOException e) {
                    log.error("SsmCardChangeController.add:", e);
                } catch (SmsException e) {
                    log.error("SsmCardChangeController.add:", e);
                }
            }else {
                genericResponse.setCodeAndMsg(ResponseCode.SMS_NO_OK);
            }
        }
        return genericResponse;
    }



    @ApiOperation(value = "加价转疗程单据业务员修改 门店端-加价-加价转疗程单据业务员修改",httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/updateStaff")
    public GenericResponse updateStaff(@RequestBody CardChangeListRequest cardChangeListRequest){
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
//        cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            i = ssmCardChangeService.updateStaff(cardChangeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCardChangeController.updateStaff", 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 CardChangeListRequest cardChangeListRequest){
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
//        cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        cardChangeListRequest.setStoreName(SessionHelp.getStoreName(session));
//        cardChangeListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        try {
            i = ssmCardChangeService.cancel(cardChangeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCardChangeController.cancel", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
