package com.sifang.modules.customer.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerService;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.customer.entity.Customer;
import com.sifang.modules.customer.entity.CustomerWarranty;
import com.sifang.modules.order.entity.CrmCustomerWarrantyCard;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.platform.service.WarrantyService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/customer")
public class CustomerController extends BaseController {
    @Autowired
    CustomerService customerService;
    @Autowired
    private WarrantyService warrantyService;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "customerList")
    public String CustomerList(HttpServletRequest request, Model model) {
        return "modules/yangguang/customer/customer";
    }

    @RequestMapping(value = "toPsDetail")
    public String toPsDetail(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String userId = request.getParameter("userId");
        Record ps = customerService.getPsDetail(id);
        String date = ps.getStr("expect_install_date");
        if (StringUtil.isNotBlank(date)) {
            ps.set("expect_install_date", date.substring(0, 10));
        }
        String type = ps.getStr("connect_type");
        if (StringUtil.isNotBlank(type)) {
            if (type.equals("1")) {
                ps.set("connect_type", "全额上网");
            } else if (type.equals("2")) {
                ps.set("connect_type", "自发自用，余电上网");
            } else if (type.equals("3")) {
                ps.set("connect_type", "自发自用，无馈网");
            } else if (type.equals("4")) {
                ps.set("connect_type", "离网");
            } else {
                ps.set("connect_type", "");
            }

        }
        List<Record> imgs = customerService.getImgs(id);
        List<Record> list = customerService.getStationDevice(id, userId);
        model.addAttribute("ps", ps);
        model.addAttribute("list", list);
        model.addAttribute("imgs", imgs);
        return "modules/yangguang/customer/psdetail";
    }

    /**
     * 添加保修卡 平台给云用户 ---注册用户不可添加
     */
    @RequestMapping(value = "addWarrantyCard")
    public String addWarrantyCard(HttpServletRequest request, Model model) {
        String userId = request.getParameter("userId");
        String id = request.getParameter("id");//用户表id
        String psId = request.getParameter("psId");
        //获取电站信息
        Record rd = customerService.getPsDetail(psId);
        Date time = DateUtils.parseDate(rd.getStr("expect_install_date"));
        //获取电站的设备信息
        if (time != null) {
            model.addAttribute("time", DateUtils.formatDate(time, "yyyy-MM-dd"));
        }
        List<Record> list = customerService.getStationDevice(psId, userId);
        model.addAttribute("rd", rd);
        model.addAttribute("list", list);
        model.addAttribute("cusId", id);
        return "modules/order/platform/addWarranty";
    }

    /**
     * 手动添加保修卡 -- 保存
     */
    @ResponseBody
    @RequestMapping(value = "savaWarrantyCard")
    public Result savaWarrantyCard(HttpServletRequest request, HttpServletResponse response, CustomerWarranty warranty) {
        String str = request.getParameter("list");
        List<CrmCustomerWarrantyCard> list = JSONObject.parseArray(str, CrmCustomerWarrantyCard.class);
        String orderId = request.getParameter("orderId");
        return warrantyService.saveWarranty(list, orderId, warranty);
    }

    /**
     * 删除电子保修卡
     */
    @ResponseBody
    @RequestMapping(value = "deleteWarrantyCard")
    public Object deleteWarrantyCard(HttpServletRequest request) {
        String cusId = request.getParameter("cusId");
        try {
            return warrantyService.deleteWarrantyCard(cusId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("202", "删除失败! " + e);
        }
    }


    @RequestMapping(value = "lookWarranty")
    public String lookWarranty(HttpServletRequest request, Model model) {
        String name = request.getParameter("name");
        String userId = request.getParameter("userId");
        Customer custo = customerService.getCustomerByUserId(userId);
        CustomerWarranty warranty = null;
        List<Record> list = null;
        if (UserConstants.USER_TYPE_ONE.equals(custo.getUserType())) {
            //云用户
            warranty = warrantyService.getByCustomerId(custo.getId());
            list = warrantyService.getByWarrantyId(warranty.getId());
        } else if (UserConstants.USER_TYPE_TWO.equals(custo.getUserType())) {
            //查询是否有工单
            Record card = orderService.getOrderByCustomerId(name, custo.getId());
            if (card != null) {
                warranty = warrantyService.getByOrderId(card.getStr("id"));
                list = customerService.getWarranty(card.getStr("id"));
            }
        }
        model.addAttribute("card", warranty);
        model.addAttribute("list", list);
        return "modules/yangguang/customer/warrantycard";
    }

    @ResponseBody
    @RequestMapping(value = "getCustomerList")
    public Object getCustomerList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        customerService.getCustomerList(page, map);
        return page.renderJson();
    }

    @ResponseBody
    @RequestMapping(value = "ProhibitCustomer")
    public Object ProhibitCustomer(HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtil.isNotEmpty(id)) {
            customerService.ProhibitCustomer(id);
            return Result.ok();
        }
        return Result.fail("501", "数据为空");
    }

    @ResponseBody
    @RequestMapping(value = "updateRemark")
    public Object updateRemark(HttpServletRequest request) {
        String id = request.getParameter("id");
        String remark = request.getParameter("remark");
        if (StringUtil.isNotEmpty(id)) {
            customerService.updateRemark(id, remark);
            return Result.ok();
        }
        return Result.fail("501", "数据为空");
    }


}
