package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreManagement;
import com.ssm.manage.query.StoreManagementRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value="门店管理关系controller",tags={"门店管理关系相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeManagement")
@Slf4j
public class SsmStoreManagementController {

    @Autowired
    private SsmStoreManagementService ssmStoreManagementService;

    @ApiOperation(value = "查询当前门店所管理下的门店", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/selectList")
    public GenericResponse selectList(@RequestBody StoreManagementRequest storeManagementRequest, HttpSession session) {
        GenericResponse<StoreManagement> genericResponse = new GenericResponse();
        storeManagementRequest.setStoreIds(SessionHelp.getStoreId(session));
        List<StoreManagement> list = new ArrayList<>();
        try {
            list = ssmStoreManagementService.selectList(storeManagementRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.selectList:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }


    @ApiOperation(value = "添加门店管理下门店", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody List<StoreManagement> storeManagements, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();

//        sysUser.setMobilePhone(CommonUtils.mobileFormat(sysUser.getMobilePhone()));
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreManagementService.saveBatch(storeManagements,userNum);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmSysUserController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


}
