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.entity.BaseCollectionType;
import com.ssm.manage.query.CollectionTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCollectionTypeService;
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 java.util.ArrayList;
import java.util.List;

@Api(value="支付方式controller",tags={"支付方式相关接口"})
@RestController
@RequestMapping("/demay/ssm/ct")
@Slf4j
public class SsmCollectionTypeController {
    @Autowired
    private SsmCollectionTypeService collectionTypeService;

    @ApiOperation(value = "支付方式分页查询  平台管理-基础档案-支付方式-支付方式分页查询",httpMethod = "POST", notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse<BaseCollectionType> selectPage(@RequestBody CollectionTypeRequest collectionTypeRequest){
        PageResponse<BaseCollectionType> pageResponse = new PageResponse<>();
        PageInfo<BaseCollectionType> pageInfo = new PageInfo<>();
        try {
            pageInfo = collectionTypeService.selectPage(collectionTypeRequest);
        }catch (Exception e){
            log.error("SsmCollectionTypeController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "添加支付方式  平台管理-基础档案-支付方式-添加支付方式", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody CollectionTypeRequest collectionTypeRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(collectionTypeRequest.getCollectionTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "支付方式名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(collectionTypeRequest.getCollectionType())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "支付方式编码");
            return genericResponse;
        }
        int i = 0;
        try {
            i = collectionTypeService.saveCollectionType(collectionTypeRequest);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.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(value = "/update", method = RequestMethod.POST)
    public GenericResponse update(@RequestBody CollectionTypeRequest collectionTypeRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(collectionTypeRequest.getCollectionTypeName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "支付方式名称");
            return genericResponse;
        }
//        if (StringUtils.isBlank(collectionTypeRequest.getCollectionType())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "支付方式编码");
//            return genericResponse;
//        }
        if (collectionTypeRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
        }
        int i = 0;
        try {
            i = collectionTypeService.updateCollectionType(collectionTypeRequest);
        } catch (Exception e) {
            log.error("SsmSysRoleController.update:", 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(value = "/updateState", method = RequestMethod.POST)
    public GenericResponse updateState(@RequestBody CollectionTypeRequest collectionTypeRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (collectionTypeRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
        }
        int i = 0;
        try {
            i = collectionTypeService.updateCollectionTypeState(collectionTypeRequest);
        } catch (Exception e) {
            log.error("SsmSysRoleController.updateState:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "查询可用支付方式list  (门店管理-开单页面-支付方式查询)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public GenericResponse selectList() {
        GenericResponse genericResponse = new GenericResponse();
        List<BaseCollectionType> list = new ArrayList<>();
        try {
            list = collectionTypeService.selectList();
        } catch (Exception e) {
            log.error("SsmSysRoleController.updateState:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }
}
