package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.bweb.excelListener.ImportFleetOilCardCarListener;
import com.bweb.excelListener.ImportFleetOilCardUserListener;
import com.bweb.model.ImportFleetOilCardCarModel;
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.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighFleetOilCard;
import com.hai.entity.HighFleetOilCardCar;
import com.hai.enum_type.FleetOilCardStatusEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighFleetOilCardCarService;
import com.hai.service.HighFleetOilCardService;
import com.hai.service.HighFleetOilCardUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 = "/fleetOilCardCar")
@Api(value = "车队油卡用户")
public class HighFleetOilCardCarController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighFleetOilCardCarService fleetOilCardCarService;

    @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, "油卡状态异常");
            }
            ImportFleetOilCardCarListener fleetOilCardCarListener = new ImportFleetOilCardCarListener();
            fleetOilCardCarListener.initData(userModel, fleetOilCardCarService, fleetOilCard);
            EasyExcel.read(files.getInputStream(), ImportFleetOilCardCarModel.class, fleetOilCardCarListener).sheet().doRead();

            Map<String, Object> map = new HashMap<>();
            map.put("errorData", fleetOilCardCarListener.getErrorData());
            map.put("errorTotal", fleetOilCardCarListener.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, "");
            }

            HighFleetOilCardCar cardCar = fleetOilCardCarService.getDetailById(body.getLong("id"));
            if (cardCar == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            cardCar.setStatus(0);
            fleetOilCardCarService.editData(cardCar);

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

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

    @RequestMapping(value = "/getCarList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询车列表")
    public ResponseData getCarList(@RequestParam(name = "fleetOilCardNo", required = false) String fleetOilCardNo,
                                    @RequestParam(name = "carLicensePlate", required = false) String carLicensePlate,
                                    @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 {

            Map<String, Object> param = new HashMap<>();
            param.put("fleetOilCardNo", fleetOilCardNo);
            param.put("carLicensePlate", carLicensePlate);
            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<>(fleetOilCardCarService.getCarList(param)));

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

}
