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.SysCompany;
import com.ssm.manage.query.SysCompanyRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysCompanyService;
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;

/**
 * (sys_company)表控制层
 *
 * @author makejava
 * @since 2021-12-24 10:37:16
 */
@Api(value = "公司组织Controller",tags = {"公司组织"})
@RestController
@RequestMapping("/demay/ssm/syscompany")
@Slf4j
public class SsmSysCompanyController {

    @Autowired
    private SsmSysCompanyService ssmSysCompanyService;

    @ApiOperation(value = "分页查询公司组织",notes = "",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping(value = "/page")
    public PageResponse<SysCompany> selectPage(@RequestBody SysCompanyRequest sysCompanyRequest){
        PageResponse<SysCompany> pageResponse = new PageResponse<>();
        PageInfo<SysCompany> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmSysCompanyService.selectPage(sysCompanyRequest);
        }catch (Exception e){
            log.error("SsmSysCompanyController.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 SysCompany sysCompany, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysCompany.getCompanyCode())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司编码");
            return genericResponse;
        }
        if (StringUtils.isBlank(sysCompany.getCompanyName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司名称");
            return genericResponse;
        }
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysCompanyService.saveSysCompany(sysCompany,userNum);
        } catch (Exception e) {
            log.error("SsmSysCompanyController.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 SysCompany sysCompany, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysCompany.getCompanyCode())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司编码");
            return genericResponse;
        }
        if (StringUtils.isBlank(sysCompany.getCompanyName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "公司名称");
            return genericResponse;
        }
        if (sysCompany.getCompanyId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"CompanyId");
        }
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysCompanyService.updateSysCompany(sysCompany,userNum);
        } catch (Exception e) {
            log.error("SsmSysCompanyController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
