package com.huiju.foyo.controller;

import com.huiju.foyo.common.BaseController;
import com.huiju.foyo.common.ConstantFoyo;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.TdCompanyInfo;
import com.huiju.foyo.service.ITdCompanyInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
    * 公司信息  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/")
public class TdCompanyInfoController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TdCompanyInfoController.class);

    @Autowired
    public ITdCompanyInfoService iTdCompanyInfoService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/company/info",method= RequestMethod.GET)
    public ResponseBean tdCompanyInfoList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdCompanyInfoService.list());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdCompanyInfoList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param tdCompanyInfo 实体对象
     * @return
     */
    @RequestMapping(value="/admin/company/add",method= RequestMethod.POST)
    public ResponseBean tdCompanyInfoAdd(@RequestBody TdCompanyInfo tdCompanyInfo){
        ResponseBean responseBean = new ResponseBean();
        try {

            List<TdCompanyInfo> tdCompanyInfoList = iTdCompanyInfoService.list();
            if (tdCompanyInfoList.size() > 0){
                TdCompanyInfo tdCompanyInfoUpdate = tdCompanyInfoList.get(0);
                tdCompanyInfoUpdate.setUpdateTime(LocalDateTime.now());
                tdCompanyInfoUpdate.setCompanyImg(tdCompanyInfo.getCompanyImg());
                if (iTdCompanyInfoService.updateById(tdCompanyInfoUpdate)){
                    responseBean.addSuccess(tdCompanyInfoUpdate);
                }else {
                    responseBean.addError("fail");
                }
            }else {
                tdCompanyInfo.setCreateBy(ConstantFoyo.USER_ADMIN);
                tdCompanyInfo.setCreateTime(LocalDateTime.now());
                if (iTdCompanyInfoService.save(tdCompanyInfo)){
                    responseBean.addSuccess(tdCompanyInfo);
                }else {
                    responseBean.addError("fail");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdCompanyInfoAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/tdCompanyInfo/{id}", method= RequestMethod.DELETE)
    public ResponseBean tdCompanyInfoDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTdCompanyInfoService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdCompanyInfoDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param tdCompanyInfo 实体对象
     * @return
     */
    @RequestMapping(value="/tdCompanyInfo/{id}",method= RequestMethod.PUT)
    public ResponseBean tdCompanyInfoUpdate(@PathVariable Integer id,
                                        @RequestBody TdCompanyInfo tdCompanyInfo){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdCompanyInfoService.updateById(tdCompanyInfo)){
                responseBean.addSuccess(tdCompanyInfo);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdCompanyInfoUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/tdCompanyInfo/{id}",method= RequestMethod.GET)
    public ResponseBean tdCompanyInfoGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdCompanyInfoService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdCompanyInfoDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
