package com.sifang.modules.smallprogram;

import com.sifang.modules.sunshine.SystemSetting.dao.SiteInformationDao;
import com.sifang.modules.sunshine.SystemSetting.service.FittingAmountService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.web.BaseController;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/main/crmFitAmount")
public class CrmFittingAmountController extends BaseController {
    @Autowired
    FittingAmountService fittingAmountService;
    @Autowired
    SiteInformationDao siteInformationDao;
    protected Logger logger = LoggerFactory.getLogger(CrmFittingAmountController.class);
    /**
     * 添加保存充值
     * @param request
     * @return
     */
    @RequestMapping(value = "crmCusSave")
    @ResponseBody
    public Object siteSave(HttpServletRequest request){
        Map<String,String> map = getParams(request);

        String customerId = map.get("customerId");
        if(StringUtil.isBlank(customerId)){
            return Result.fail("202","客户id为空");
        }

        String siteId = request.getParameter("siteId");
        if(StringUtil.isBlank(siteId)){
            siteId = siteInformationDao.getSiteIdByCustomerId(customerId);
        }
        if(StringUtil.isBlank(siteId)){
            logger.error("crmCusSave  :  没有刚用户所绑定的服务商 ： customerId: {}",customerId);
            return Result.fail("203","没有绑定服务商");
        }
        map.put("siteId",siteId);


        return fittingAmountService.save(map);
    }

    /**
     * CRM 流程审批通过 -- 扣款
    */
    @ResponseBody
    @RequestMapping("siteFittingDeduction")
    public Object siteFittingDeduction(HttpServletRequest request){
        String applyId = request.getParameter("applyId");
        String customerId = request.getParameter("customerId");

        if(StringUtil.isBlank(applyId)){
            return Result.fail("202","备件申请id为空");
        }
        if(StringUtil.isBlank(customerId)){
            return Result.fail("202","客户信息为空");
        }
            try {
                return fittingAmountService.siteFittingDeduction(applyId,customerId);
            }catch (Exception e){
                e.printStackTrace();
                return Result.fail("505","扣款失败");
            }
    }


    /**
     * 单独扣款备件额度
    */
    @ResponseBody
    @RequestMapping("siteDeduction")
    public Object siteDeduction(HttpServletRequest request){
        String customerId = request.getParameter("customerId");
        String amount = request.getParameter("amount");

        try {
            return  fittingAmountService.siteDeduction(customerId,amount);
        }catch (Exception e){
            logger.error("单独扣款备件额度 失败 Exception ：{}",e);
            return Result.fail("505","扣款失败");
        }


    }


}
