package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.bweb.excelListener.ImportCouponListener;
import com.bweb.excelListener.ImportFleetOilCardUserListener;
import com.bweb.model.ImportCouponModel;
import com.bweb.model.ImportFleetOilCardUserModel;
import com.github.pagehelper.PageHelper;
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.AESEncodeUtil;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighCompanyTwoPwd;
import com.hai.entity.HighFleetOilCard;
import com.hai.entity.HighFleetOilCardUser;
import com.hai.enum_type.FleetOilCardStatusEnum;
import com.hai.enum_type.OilCardStatusEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCompanyTwoPwdService;
import com.hai.service.HighFleetOilCardService;
import com.hai.service.HighFleetOilCardUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Controller
@RequestMapping(value = "/fleetOilCardUser")
@Api(value = "车队油卡用户")
public class HighFleetOilCardUserController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighFleetOilCardUserService fleetOilCardUserService;

    @Resource
    private HighFleetOilCardService fleetOilCardService;

    @RequestMapping(value = "/importData", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "导入数据")
    public ResponseData importData(@RequestParam(name = "cardNo", required = true) String cardNo,
                                   @RequestParam(value = "file" , required = false) MultipartFile files) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel.getBsOrganization() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "权限不足");
            }
            // 查询油卡
            HighFleetOilCard fleetOilCard = fleetOilCardService.getFleetOilCardByCardNo(cardNo);
            if (fleetOilCard == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到车队油卡信息");
            }
            if (!fleetOilCard.getStatus().equals(FleetOilCardStatusEnum.status1.getStatus())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡状态异常");
            }
            ImportFleetOilCardUserListener fleetOilCardUserListener = new ImportFleetOilCardUserListener();
            fleetOilCardUserListener.initData(userModel, fleetOilCard, fleetOilCardUserService);
            EasyExcel.read(files.getInputStream(), ImportFleetOilCardUserModel.class, fleetOilCardUserListener).sheet().doRead();

            Map<String, Object> map = new HashMap<>();
            map.put("errorData", fleetOilCardUserListener.getErrorData());
            map.put("errorTotal", fleetOilCardUserListener.getErrorData().size());
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestBody JSONObject body) {
        try {
            if (body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            fleetOilCardUserService.delete(body.getLong("id"));

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

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

    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户列表")
    public ResponseData getUserList(@RequestParam(name = "fleetOilCardNo", required = false) String fleetOilCardNo,
                                    @RequestParam(name = "contactName", required = false) String contactName,
                                    @RequestParam(name = "contactPhone", required = false) String contactPhone,
                                    @RequestParam(name = "createTimeS", required = false) Long createTimeS,
                                    @RequestParam(name = "createTimeE", required = false) Long createTimeE,
                                    @RequestParam(name = "status", required = false) Integer status,
                                    @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                    @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            Map<String, Object> param = new HashMap<>();
            param.put("fleetOilCardNo", fleetOilCardNo);
            param.put("contactName", contactName);
            param.put("contactPhone", contactPhone);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("status", status);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(fleetOilCardUserService.getUserList(param)));

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

    @RequestMapping(value = "/getUserOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户订单列表")
    public ResponseData getUserOrderList(@RequestParam(name = "orgId", required = false) Long orgId,
                                         @RequestParam(name = "cardNo", required = false) String cardNo,
                                         @RequestParam(name = "goodsName", required = false) String goodsName,
                                         @RequestParam(name = "status", required = false) Integer status,
                                         @RequestParam(name = "payTimeS", required = false) String payTimeS,
                                         @RequestParam(name = "payTimeE", required = false) String payTimeE,
                                         @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                         @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);

            Map<String, Object> param = new HashMap<>();
            param.put("companyId", userModel.getBsCompany().getId());

            if (userModel.getSecUser().getAdminFlag() != 1) {
                param.put("orgId", userModel.getBsOrganization().getId());
            } else {
                param.put("orgId", orgId);
            }
            param.put("status", status);
            param.put("cardNo", cardNo);
            param.put("goodsName", goodsName);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);

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

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

}
