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.BaseStores;
import com.ssm.manage.entity.RechargeDemotioneingRun;
import com.ssm.manage.query.RechargeDemotioneingRunRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmRechargeDemotioneingRunService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value="门店充值卡升降级controller",tags={"门店充值卡升降级"})
@RestController
@RequestMapping("/demay/ssm/rechargedemotioneingrun")
@Slf4j
public class SsmRechargeDemotioneingRunController {

    @Autowired
    private SsmRechargeDemotioneingRunService ssmRechargeDemotioneingRunService;


    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "分页获取可升降级卡", httpMethod = "POST", response = PageResponse.class, notes = "分页获取用户信息")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse<RechargeDemotioneingRun> selectByPage(@RequestBody RechargeDemotioneingRunRequest rechargeDemotioneingRunRequest, HttpSession session) {
        PageResponse<RechargeDemotioneingRun> pageResponse = new PageResponse<>();
        PageInfo<RechargeDemotioneingRun> pageInfo;
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                rechargeDemotioneingRunRequest.setStoreId(pagesorganization.getId());
                break;
            }
        }
        try {
            pageInfo = ssmRechargeDemotioneingRunService.selectByPage(rechargeDemotioneingRunRequest);

        } catch (Exception e) {
            log.error("SsmSysUserController.selectByPage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页本店全部升降级关系信息", httpMethod = "POST", response = PageResponse.class, notes = "分页获取用户信息")
    @RequestMapping(value = "/pages", method = RequestMethod.POST)
    public PageResponse<RechargeDemotioneingRun> selectByPages(@RequestBody RechargeDemotioneingRunRequest rechargeDemotioneingRunRequest, HttpSession session) {
        PageResponse<RechargeDemotioneingRun> pageResponse = new PageResponse<>();
        PageInfo<RechargeDemotioneingRun> pageInfo;
//        Long storeId = SessionHelp.getStoreId(session);
//        rechargeDemotioneingRunRequest.setStoreId(storeId);

        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                rechargeDemotioneingRunRequest.setStoreId(pagesorganization.getId());
                break;
            }
        }
        try {
            pageInfo = ssmRechargeDemotioneingRunService.selectByPages(rechargeDemotioneingRunRequest);
        } catch (Exception e) {
            log.error("SsmSysUserController.selectByPage:", 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(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody RechargeDemotioneingRun rechargeDemotioneingRun, HttpSession session) {
        rechargeDemotioneingRun.setStoreId(SessionHelp.getStoreId(session));
        rechargeDemotioneingRun.setStoreName(SessionHelp.getStoreName(session));
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getRechargeTypeId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型ID");
            return genericResponse;
        }
//        if (StringUtils.isBlank(rechargeDemotioneingRun.getRechargeTypeNum())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型编码");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getRechargeTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getNewRechargeTypeId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型ID");
            return genericResponse;
        }
//        if (StringUtils.isBlank(rechargeDemotioneingRun.getNewRechargeTypeNum())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型编码");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getNewRechargeTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型名称");
            return genericResponse;
        }
        if(rechargeDemotioneingRun.getRechargeTypeId().equals(rechargeDemotioneingRun.getNewRechargeTypeId()) || rechargeDemotioneingRun.getRechargeTypeId() == rechargeDemotioneingRun.getNewRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.NOT_EQUALS_REQUIREMENTS_S_new, "原充值类型ID和升降级充值类型ID");
            return genericResponse;
        }
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getStoreId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getStoreName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店名称");
            return genericResponse;
        }
//        if (null != rechargeDemotioneingRun.getDiscount()) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型享受优惠");
//            return genericResponse;
//        }
//        if (null != rechargeDemotioneingRun.getNewDiscount()) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型享受优惠");
//            return genericResponse;
//        }
        if (null == rechargeDemotioneingRun.getType()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "充值类型(0=升级，1=降级)");
            return genericResponse;
        }

        if (null == rechargeDemotioneingRun.getStatus()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "状态");
            return genericResponse;
        }

//        if (null != rechargeDemotioneingRun.getRechargeMoney()) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级标准为空");
//            return genericResponse;
//        }
        int i = ssmRechargeDemotioneingRunService.saveRechargeDemotioneingRun(rechargeDemotioneingRun);
        if (i > 0) {
            if(i==2){
                genericResponse.setCodeAndMsg(ResponseCode.DATA_ALREADY_EXISTS_S,"该卡对应的升降级数据");
            }else{
                genericResponse.setCodeAndMsg(ResponseCode.OK);
            }

        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改可升降级卡", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public GenericResponse update(@RequestBody RechargeDemotioneingRun rechargeDemotioneingRun, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getRechargeTypeId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型ID");
            return genericResponse;
        }
//        if (StringUtils.isBlank(rechargeDemotioneingRun.getRechargeTypeNum())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型编码");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getRechargeTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getNewRechargeTypeId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型ID");
            return genericResponse;
        }
//        if (StringUtils.isBlank(rechargeDemotioneingRun.getNewRechargeTypeNum())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型编码");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getNewRechargeTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(String.valueOf(rechargeDemotioneingRun.getStoreId()))) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (StringUtils.isBlank(rechargeDemotioneingRun.getStoreName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店名称");
            return genericResponse;
        }
//        if (null != rechargeDemotioneingRun.getDiscount()) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "原充值类型享受优惠");
//            return genericResponse;
//        }
//        if (null != rechargeDemotioneingRun.getNewDiscount()) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升降级充值类型享受优惠");
//            return genericResponse;
//        }
        if (null == rechargeDemotioneingRun.getType()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "充值类型");
            return genericResponse;
        }
        if (null == rechargeDemotioneingRun.getId()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "ID");
            return genericResponse;
        }

        if (null == rechargeDemotioneingRun.getStatus()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "状态");
            return genericResponse;
        }
        int i = ssmRechargeDemotioneingRunService.updateRechargeDemotioneingRun(rechargeDemotioneingRun);
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
    @ApiOperation(value = "修改升降级卡状态", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public GenericResponse updateStatus(@RequestBody RechargeDemotioneingRun rechargeDemotioneingRun, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (null == rechargeDemotioneingRun.getId()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "ID");
            return genericResponse;
        }
        if (null == rechargeDemotioneingRun.getStatus()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "状态");
            return genericResponse;
        }
        int i = ssmRechargeDemotioneingRunService.updateRechargeDemotioneingRun(rechargeDemotioneingRun);
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

}
