package com.izouma.awesomeadmin.web;

import java.util.*;

import com.izouma.awesomeadmin.util.ExportExcelUtil;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import io.rong.RongCloud;
import io.rong.models.CheckOnlineResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.CustomerService;
import com.izouma.awesomeadmin.service.CustomerServiceService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * controller类
 */
@Controller
@RequestMapping("/customerService")
@RequiresAuthentication
public class CustomerServiceController {

    @Autowired
    private CustomerServiceService customerServiceService;

    private RongCloud rongCloud = RongCloud.getInstance(PropertiesFileLoader.getProperties("rongyunappkey"), PropertiesFileLoader.getProperties("rongyunappsecret"));

    /**
     * <p>获取全部记录。</p>
     */
    @RequiresAuthentication
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public Result all(CustomerService record) {
        List<CustomerService> pp = customerServiceService.getCustomerServiceList(record);
        return new Result(true, pp);
    }

    /**
     * <p>根据Id。</p>
     */
    @RequestMapping(value = "/getCustomerService", method = RequestMethod.GET)
    @ResponseBody
    public Result getCustomerService(@RequestParam(required = false, value = "id") String id) {
        CustomerService data = customerServiceService.getCustomerServiceById(id);
        return new Result(true, data);
    }

    /**
     * <p>根据条件获取。</p>
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(CustomerService record) {
        CustomerService data = customerServiceService.getCustomerService(record);
        return new Result(true, data);
    }


    /**
     * <p>分页查询。</p>
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Result page(Page page, CustomerService record) {
        Map<String, Object> result = new HashMap<>();

        List<CustomerService> pp = customerServiceService.getCustomerServiceByPage(page, record);

        result.put(AppConstant.PAGE, page);
        result.put("pp", pp);
        return new Result(true, result);
    }


    /**
     * <p>保存。</p>
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(CustomerService record) {
        boolean num = customerServiceService.createCustomerService(record);
        if (num) {
            return new Result(true, record.getId());
        }
        return new Result(false, "保存异常");
    }

    /**
     * <p>更新信息。</p>
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateCustomerService(CustomerService record) {
        boolean num = customerServiceService.updateCustomerService(record);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");
    }

    /**
     * <p>删除。</p>
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteCustomerService(CustomerService record) {

        boolean num = customerServiceService.deleteCustomerService(record);
        if (num) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");
    }

    /**
     * 导出Excel
     *
     * @param request
     * @param response
     * @param record
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, CustomerService record) throws Exception {

        List<CustomerService> customerServices = customerServiceService.getCustomerServiceList(record);


        String sheetName = "customer_service";
        String titleName = "店铺客服数据表";
        String fileName = "店铺客服表";
        int columnNumber = 14;
        int[] columnWidth = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
        String[] columnName = {"", "删除标识", "更新时间", "更新人", "创建时间", "创建人", "店铺", "客服名", "密码", "登录名", "头像", "可用标识", "状态", "登录时间"};
        String[][] dataList = new String[customerServices.size()][14];

        for (int i = 0; i < customerServices.size(); i++) {

            dataList[i][0] = String.valueOf(customerServices.get(i).getId());
            dataList[i][1] = String.valueOf(customerServices.get(i).getDelFlag());
            dataList[i][2] = String.valueOf(customerServices.get(i).getUpdateTime());
            dataList[i][3] = String.valueOf(customerServices.get(i).getUpdateUser());
            dataList[i][4] = String.valueOf(customerServices.get(i).getCreateTime());
            dataList[i][5] = String.valueOf(customerServices.get(i).getCreateUser());
            dataList[i][6] = String.valueOf(customerServices.get(i).getStoreId());
            dataList[i][7] = String.valueOf(customerServices.get(i).getServiceName());
            dataList[i][8] = String.valueOf(customerServices.get(i).getPassword());
            dataList[i][9] = String.valueOf(customerServices.get(i).getUsername());
            dataList[i][10] = String.valueOf(customerServices.get(i).getIcon());
            dataList[i][11] = String.valueOf(customerServices.get(i).getUseFlag());
            dataList[i][12] = String.valueOf(customerServices.get(i).getStatusFlag());
            dataList[i][13] = String.valueOf(customerServices.get(i).getLoginTime());
        }


        ExportExcelUtil.ExportWithResponse(sheetName, titleName, fileName,
                columnNumber, columnWidth, columnName, dataList, response);


    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(HttpServletRequest request,
                        @RequestParam(required = true, value = "username") String username, @RequestParam(required = true, value = "password") String password) {

        CustomerService record = new CustomerService();
        record.setUsername(username);
        record.setPassword(password);

        record = customerServiceService.getCustomerService(record);

        if (record != null) {

            request.getSession(true).setAttribute("customerServiceName", username);

            CustomerService update = new CustomerService();
            update.setId(record.getId());
            update.setLoginTime(new Date());

            customerServiceService.updateCustomerService(update);

            return new Result(true, record);
        }

        return new Result(false, "用户名或密码错误");
    }

    /**
     * 获取最近登录客服
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/recently", method = RequestMethod.GET)
    @ResponseBody
    public Result recently(CustomerService record) {
        List<CustomerService> pp = customerServiceService.getRecentlyLoginList(record);

        for (CustomerService customerService : pp) {
            try {

                CheckOnlineResult checkOnlineResult = rongCloud.user.checkOnline(customerService.getUsername());
                customerService.setCheckOnlineResult(checkOnlineResult);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return new Result(true, pp);
    }


}

