package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.HighGasClassGroup;
import com.hai.entity.HighGasClassGroupTask;
import com.hai.enum_type.GasClassGroupTaskStatus;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighGasClassGroupService;
import com.hai.service.HighGasClassGroupTaskService;
import com.hai.service.HighMerchantStoreService;
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 javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加油站班组
 * @author hurui
 */
@Controller
@RequestMapping(value = "/gasClassGroup")
@Api(value = "加油站班组")
public class HighGasClassGroupController {

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

    @Resource
    private HighGasClassGroupService gasClassGroupService;

    @Resource
    private HighGasClassGroupTaskService gasClassGroupTaskService;

    @Resource
    private HighMerchantStoreService merchantStoreService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/editClassGroup", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑班组")
    public ResponseData editClassGroup(@RequestBody JSONObject body) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getMerchantStore() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }
            if (body == null
                    || StringUtils.isBlank(body.getString("name"))
                    || StringUtils.isBlank(body.getString("principalName"))
                    || StringUtils.isBlank(body.getString("principalPhone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            HighGasClassGroup gasClassGroup;

            if (body.getLong("id") != null) {
                // 查询班组
                gasClassGroup = gasClassGroupService.getDetailById(body.getLong("id"));
                if (gasClassGroup == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            } else {
                gasClassGroup = new HighGasClassGroup();
                gasClassGroup.setMerchantStoreId(userInfoModel.getMerchantStore().getId());
                gasClassGroup.setMerchantStoreName(userInfoModel.getMerchantStore().getStoreName());
            }

            gasClassGroup.setName(body.getString("name"));
            gasClassGroup.setPrincipalName(body.getString("principalName"));
            gasClassGroup.setPrincipalPhone(body.getString("principalPhone"));
            gasClassGroupService.editGroup(gasClassGroup);

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

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

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

            Map<String, Object> param = new HashMap<>();
            param.put("gasClassGroupId", body.getLong("id"));
            param.put("status", GasClassGroupTaskStatus.status1.getStatus());
            List<HighGasClassGroupTask> groupTaskList = gasClassGroupTaskService.getGroupTaskList(param);
            if (groupTaskList.size() > 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "班组有任务进行中，暂时无法删除");
            }

            gasClassGroupService.delGroup(body.getLong("id"));

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

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

    @RequestMapping(value = "/getClassGroupById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询班组详情")
    public ResponseData getClassGroupById(@RequestParam(name = "groupId", required = true) Long groupId) {
        try {

            return ResponseMsgUtil.success(gasClassGroupService.getDetailById(groupId));

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

    @RequestMapping(value = "/getClassGroupList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询班组列表")
    public ResponseData getClassGroupList(@RequestParam(name = "name", required = false) String name,
                                          @RequestParam(name = "principalName", required = false) String principalName,
                                          @RequestParam(name = "principalPhone", required = false) String principalPhone,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getMerchantStore() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            Map<String,Object> param = new HashMap<>();
            param.put("merchantStoreId", userInfoModel.getMerchantStore().getId());
            param.put("name", name);
            param.put("principalName", principalName);
            param.put("principalPhone", principalPhone);

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

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

}
