package com.bweb.controller.recharge;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;

import com.hfkj.entity.RechargePrice;
import com.hfkj.model.ResponseData;
import com.hfkj.service.recharge.PriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/price")
@Api(value="话费金额")
public class PriceController {

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

    @Resource
    private PriceService priceService;

    @RequestMapping(value="/getListPrice",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListPrice(
                                     @RequestParam(value = "type" , required = false) Integer type,
                                     @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                     @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("type", type);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(priceService.getList(map)));

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

    @RequestMapping(value = "/editPrice", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑金额")
    public ResponseData editPrice(@RequestBody RechargePrice body) {
        try {

            if (body == null
                    || body.getPrice() == null
                    || body.getPayPrice() == null
                    || body.getSort() == null
                    || body.getType() == null
                  ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            RechargePrice rechargePrice;

            if (body.getId() != null) {
                // 查询品牌
                rechargePrice = priceService.queryDetail(body.getId());
                if (rechargePrice == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.CONTENT_NOT_FOUND, "");
                }
            } else {
                rechargePrice = new RechargePrice();
                rechargePrice.setCreateTime(new Date());
            }

            rechargePrice.setUpdateTime(new Date());
            rechargePrice.setPayPrice(body.getPayPrice());
            rechargePrice.setPrice(body.getPrice());
            rechargePrice.setType(body.getType());
            rechargePrice.setSort(body.getSort());
            rechargePrice.setStatus(1);

            if (body.getId() != null) {
                priceService.update(rechargePrice);
            } else {
                priceService.create(rechargePrice);
            }

            return ResponseMsgUtil.success("操作成功");

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

    }

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

            priceService.delete(id , false);

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

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