package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.OutRechargePrice;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.OutRechargePriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/outRechargePrice")
@Api(value = "查询充值金额接口")
public class OutRechargePriceController {

    private static Logger log = LoggerFactory.getLogger(HighMerchantStoreController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private OutRechargePriceService outRechargePriceService;

    @RequestMapping(value = "/getListOutRechargePrice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询充值金额列表")
    public ResponseData getListOutRechargePrice(
            @RequestParam(value = "type", required = false) Integer type ,
            @RequestParam(value = "showType", required = false) Integer showType ,
            @RequestParam(value = "rechargeType", required = false) Integer rechargeType ,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize, HttpServletRequest request
    ) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String, String> map = new HashMap<>();

            if (type != null ) {
                map.put("type", String.valueOf(type));
            }

            if (showType != null ) {
                map.put("showType", String.valueOf(showType));
            }

            if (rechargeType != null ) {
                map.put("rechargeType", String.valueOf(rechargeType));
            }

            map.put("companyId" , String.valueOf(userInfoModel.getBsCompany().getId()));

            PageHelper.startPage(pageNum, pageSize);

            List<OutRechargePrice> outRechargePrices = outRechargePriceService.getListRechargePrice(map);

            return   ResponseMsgUtil.success(new PageInfo<>(outRechargePrices));
        } catch (Exception e) {
            log.error("OutRechargePriceController --> getListUser() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/insertPrice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增金额")
    public ResponseData insertAgent(@RequestBody OutRechargePrice outRechargePrice, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (outRechargePrice.getType() == null
                    || outRechargePrice.getPrice() == null
                    || outRechargePrice.getRealPrice() == null
                    || outRechargePrice.getShowType() == null
                    || outRechargePrice.getRechargeType() == null
                    || outRechargePrice.getSort() == null
            ) {
                log.error("SecOrganizationController -> insertPrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }


            outRechargePrice.setCompanyId(userInfoModel.getBsCompany().getId());
            outRechargePrice.setCreateTime(new Date());
            outRechargePrice.setStatus(0);

            outRechargePriceService.insertRechargePrice(outRechargePrice);

            return ResponseMsgUtil.success("新增成功");

        } catch (Exception e) {
            log.error("OutRechargePriceController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updatePrice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改金额")
    public ResponseData updatePrice(@RequestBody OutRechargePrice outRechargePrice) {
        try {
            if (outRechargePrice.getType() == null
                    || outRechargePrice.getId() == null
                    || outRechargePrice.getPrice() == null
                    || outRechargePrice.getRealPrice() == null
                    || outRechargePrice.getRechargeType() == null
                    || outRechargePrice.getShowType() == null
                    || outRechargePrice.getSort() == null
            ) {
                log.error("SecOrganizationController -> updatePrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            OutRechargePrice outRechargePrice1 = outRechargePriceService.findById(outRechargePrice.getId());

            if (outRechargePrice1 == null ) {
                log.error("SecOrganizationController -> updatePrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.UPDATE_DATA_ERROR, "");
            }

            outRechargePrice.setCompanyId(outRechargePrice1.getCompanyId());
            outRechargePrice.setStatus(0);
            outRechargePrice.setCreateTime(outRechargePrice1.getCreateTime());

            outRechargePriceService.updateRechargePrice(outRechargePrice);

            return ResponseMsgUtil.success("修改成功");

        } catch (Exception e) {
            log.error("OutRechargePriceController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询详情")
    public ResponseData findById(@RequestParam(value = "id", required = true) Long id) {
        try {

            OutRechargePrice outRechargePrice = outRechargePriceService.findById(id);
            return ResponseMsgUtil.success(outRechargePrice);

        } catch (Exception e) {
            log.error("HighUserController --> findByUserId() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/deletePrice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData deletePrice(@RequestParam(value = "id", required = true) Long id) {
        try {

           outRechargePriceService.deletePrice(id);
            return ResponseMsgUtil.success("删除成功");

        } catch (Exception e) {
            log.error("HighUserController --> findByUserId() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/onAndOffShelves", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "上下架")
    public ResponseData onAndOffShelves(@RequestParam(value = "id", required = true) Long id) {
        try {

            OutRechargePrice outRechargePrice = outRechargePriceService.findById(id);

            if (outRechargePrice.getStatus() == 1) {
                outRechargePrice.setStatus(0);
            } else {
                outRechargePrice.setStatus(1);
            }

            outRechargePriceService.updateRechargePrice(outRechargePrice);

            return ResponseMsgUtil.success("修改成功");

        } catch (Exception e) {
            log.error("HighUserController --> findByUserId() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
