package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SysCompanyStore;
import com.ssm.manage.query.SysCompanyStoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysCompanyStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.List;

/**
 * (sys_company)表控制层
 *
 * @author makejava
 * @since 2021-12-24 10:37:16
 */
@Api(value = "公司组织与门店Controller",tags = {"公司组织与门店关系"})
@RestController
@RequestMapping("/demay/ssm/syscompanystore")
@Slf4j
public class SsmSysCompanyStoreController {
    @Autowired
    private SsmSysCompanyStoreService ssmSysCompanyStoreService;

    @ApiOperation(value = "分页查询公司组织与门店关系表",notes = "",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping(value = "/page")
    public PageResponse<SysCompanyStore> selectPage(@RequestBody SysCompanyStoreRequest sysCompanyStoreRequest){
        PageResponse<SysCompanyStore> pageResponse = new PageResponse<>();
        PageInfo<SysCompanyStore> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmSysCompanyStoreService.selectPage(sysCompanyStoreRequest);
        }catch (Exception e){
            log.error("SsmSysCompanyStoreController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "添加公司组织与门店关系", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody List<SysCompanyStoreRequest> sysCompanyStores, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        for (SysCompanyStoreRequest sysCompanyStore : sysCompanyStores) {
            if (StringUtils.isBlank(sysCompanyStore.getCompanyCode())) {
                genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司编码");
                return genericResponse;
            }
            if (StringUtils.isBlank(sysCompanyStore.getCompanyName())) {
                genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司名称");
                return genericResponse;
            }
            if (StringUtils.isNotBlank(String.valueOf(sysCompanyStore.getStoreId()))){
                genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
                return genericResponse;
            }
            if (StringUtils.isNotBlank(sysCompanyStore.getStoreName())){
                genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店名称");
                return genericResponse;
            }
        }
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysCompanyStoreService.saveSysCompanyStore(sysCompanyStores,userNum);
        } catch (Exception e) {
            log.error("SsmSysCompanyStoreController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改公司组织与门店关系", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public GenericResponse update(@RequestBody SysCompanyStore sysCompanyStore, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysCompanyStore.getCompanyCode())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司编码");
            return genericResponse;
        }
        if (StringUtils.isBlank(sysCompanyStore.getCompanyName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司名称");
            return genericResponse;
        }
        if (sysCompanyStore.getCompanyStoreId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"CompanyStoreId");
        }
        if (StringUtils.isNotBlank(String.valueOf(sysCompanyStore.getStoreId()))){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (StringUtils.isNotBlank(sysCompanyStore.getStoreName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店名称");
            return genericResponse;
        }
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysCompanyStoreService.updateSysCompanyStore(sysCompanyStore,userNum);
        } catch (Exception e) {
            log.error("SsmSysCompanyStoreController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "单个删除公司与门店关系", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/deleteSingle", method = RequestMethod.POST)
    public GenericResponse deleteSigle(@RequestBody SysCompanyStoreRequest sysCompanyStoreRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (sysCompanyStoreRequest.getCompanyStoreId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"CompanyStoreId");
        }
        int i = 0;
        try {
            i = ssmSysCompanyStoreService.deleteSingleSysCompanyStore(sysCompanyStoreRequest);
        } catch (Exception e){
            log.error("SsmSysCompanyStoreController.deleteSingle:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
