package com.bweb.controller.ApiMchProduct;

import com.alibaba.fastjson.JSONObject;
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.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.*;
import com.hai.model.ResponseData;
import com.hai.openApi.service.ApiMchProductService;
import com.hai.order.type.OrderProductType;
import com.hai.service.ApiMerchantsService;
import com.hai.service.BsCompanyService;
import com.hai.service.HighMerchantService;
import com.hai.service.SecConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping(value = "/apiMchProduct")
@Api(value = "API商户产品配置")
public class ApiMchProductController {

    Logger log = LoggerFactory.getLogger(ApiMchProductController.class);

    @Resource
    private ApiMchProductService apiMchProductService;

    @Autowired
    private UserCenter userCenter;

    @Resource
    private BsCompanyService bsCompanyService;

    @Resource
    private HighMerchantService highMerchantService;

    @Resource
    private ApiMerchantsService apiMerchantsService;

    @Resource
    private SecConfigService secConfigService;


    @RequestMapping(value = "/insertMchProduct", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增产品")
    public ResponseData insertMchProduct(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            if (object == null ||
                    object.getString("mchId") == null
            ) {
                log.error("ApiProductController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            ApiMerchants merchants = apiMerchantsService.findByMchId(object.getString("mchId"));

            if (merchants == null) {
                log.error("ApiMemberProductController -> insertMemberProduct() error!","商户状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商户状态错误");
            }

            if (object.getJSONArray("productType") != null && object.getJSONArray("productType").size() > 0) {
                apiMchProductService.configMchProduct(object);
            }

            return ResponseMsgUtil.success("配置成功");

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

    @RequestMapping(value = "/getListApiMchProduct", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户产品列表")
    public ResponseData getListApiMchProduct(@RequestParam(value = "mchId", required = true) String mchId) {
        try {

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

            List<ApiMchProduct> list = apiMchProductService.getListMchProduct(map);

            for (ApiMchProduct apiMchProduct : list) {
                if (apiMchProduct.getProductType().equals(6)) {
                    apiMchProduct.setExt1(OrderProductType.getDataByType(apiMchProduct.getProductType()).getName() + productName(apiMchProduct.getCompanyId(),apiMchProduct.getMerchantId()));
                } else {
                    apiMchProduct.setExt1(OrderProductType.getDataByType(apiMchProduct.getProductType()).getName());
                }

            }

            return ResponseMsgUtil.success(list);

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

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

            ApiMchProduct apiMchProduct = apiMchProductService.findById(id);

            Map<String , Object> map = new HashMap<>();
            map.put("productType" , apiMchProduct.getProductType());
            map.put("mchId" , apiMchProduct.getMchId());

            List<ApiMchProduct> list = apiMchProductService.getListMchProduct(map);

            if (list.size() == 1) {
                secConfigService.deleteSecConfig(apiMchProduct.getMchId() , OrderProductType.getDataByType(apiMchProduct.getProductType()).getApiCode());
            }

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

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

    @RequestMapping(value = "/findMchProduct", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户产品")
    public ResponseData findMchProduct(@RequestParam(value = "id", required = true) Long id) {
        try {
            return ResponseMsgUtil.success(apiMchProductService.findById(id));

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


    @RequestMapping(value = "/editMchProduct", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改配置产品")
    public ResponseData editMchProduct(@RequestBody JSONObject object) {
        try {

            if (object == null ||
                    object.getBigDecimal("discount") == null ||
                    object.getLong("id") == null
            ) {
                log.error("ApiProductController -> configApiProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            ApiMchProduct apiMchProduct = apiMchProductService.findById(object.getLong("id"));

            apiMchProduct.setDiscount(object.getBigDecimal("discount"));
            apiMchProduct.setUpdateTime(new Date());

            apiMchProductService.updateMchProduct(apiMchProduct);

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

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

    // 编辑产品名称
    private String productName(Long companyId , Long merchantId) {
        return "(" + bsCompanyService.getCompanyById(companyId).getName() + "-" + highMerchantService.getMerchantById(merchantId).getMerchantName() + ")";
    }

}
