package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsGasClassGroup;
import com.hfkj.entity.BsGasClassGroupTask;
import com.hfkj.entity.BsGasStaff;
import com.hfkj.entity.BsMerchant;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.service.gas.BsGasClassGroupService;
import com.hfkj.service.gas.BsGasClassGroupTaskService;
import com.hfkj.service.gas.BsGasStaffService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.gas.GasClassGroupTaskStatus;
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 BsGasClassGroupController {

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

    @Resource
    private BsGasClassGroupService gasClassGroupService;

    @Resource
    private BsGasClassGroupTaskService gasClassGroupTaskService;
    @Resource
    private BsGasStaffService gasStaffService;
    @Resource
    private BsMerchantService merchantService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/editClassGroup", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑班组")
    public ResponseData editClassGroup(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userInfoModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userInfoModel == null
                    || !userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS);
            }
            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, "");
            }

            BsGasClassGroup 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 BsGasClassGroup();
                // 查询商户
                BsMerchant merchant = merchantService.getMerchant(userInfoModel.getAccount().getObjectId());
                if (merchant == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                gasClassGroup.setMerId(merchant.getId());
                gasClassGroup.setMerName(merchant.getMerName());
            }

            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<BsGasClassGroupTask> 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 {
            SecUserSessionObject userInfoModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String,Object> param = new HashMap<>();
            if (userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", userInfoModel.getAccount().getObjectId());

            } else if (userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type3.getCode())) {
                // 查询油站工作人员
                BsGasStaff gasStaff = gasStaffService.getStaffDetailById(userInfoModel.getAccount().getObjectId());
                if (gasStaff == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                param.put("merId", gasStaff.getMerId());
            }
            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);
        }
    }

}
