package com.fufan.mtsaas.modules.sysmgr.hrsys.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.hrsys.dao.SYLOGDao;
import com.fufan.mtsaas.modules.sysmgr.hrsys.model.SYLOG;
import com.github.pagehelper.ISelect;
import com.fufan.mtsaas.modules.sysmgr.hrsys.dao.SYLOGDao;
import com.fufan.mtsaas.modules.sysmgr.hrsys.model.SYLOG;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.hrsys.service.SYLOGSvc;
import com.fufan.mtsaas.util.DateFormatTool;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Liby
 * @since 2020-05-12
 */
@Service("SYLOGSvcImpl")
@Slf4j
public class SYLOGSvcImpl extends ServiceImpl<SYLOGDao, SYLOG> implements SYLOGSvc {
    @Autowired
    private SYLOGDao entitymDao;
    /**
     * 查询
     *
     * @param map
     * @return Result.class
     * */
    @Override
    public Result doQueryPage(Map<String, String> map) {
        Result result = new Result();
        //分页查询
        Page page = new Page();
        log.debug("SYLOGSvcImpl.doQueryPage parameter: {}", map);
        Map<String, String> mapData = new HashMap<>();
        String CompanyID = ParamUtil.getParamfromMap(map, "CompanyID");
        String SPINFO = ParamUtil.getParamfromMap(map, "SPINFO");

        mapData.put("CompanyID", CompanyID);
        mapData.put("SPINFO", SPINFO);

        List<SYLOG> entitys;
        ParamUtil.setPage(map, page);
        //分页处理 -- 20192021 Liby
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                entitymDao.selectList(page, mapData);
            }
        });
        String pageSize = ParamUtil.getParamfromMap(map,"pageSize");
        String currentPage = ParamUtil.getParamfromMap(map,"currentPage");
        if (null != pageSize && null != currentPage) {
            com.github.pagehelper.PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        }
        entitys = entitymDao.selectList(page, mapData);

        log.debug("SYLOGSvcImpl.doQueryPage entitys: {}", entitys);

        page.setTotal((int) total);
        page.setRecords(entitys);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);

        return result;
    }

    /**
     * 新增
     *
     * @param entitym
     * @param requestPojo
     * @return Result.class
     */
    @Override
    public Result doSave(SYLOG entitym, RequestPojo requestPojo) {
        log.debug("SYLOGSvcImpl.doSave parameter: {}", entitym);
        Result result = new Result();
        //非空验证
        if(StringUtils.isEmpty(entitym.getCompanyID()) || StringUtils.isEmpty(entitym.getSPINFO())) {
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG);
            return result;
        }

        try{
            entitym.setRegisteredPerson(requestPojo.getUserName());
            entitym.setRegisteredDT(DateFormatTool.dateToString(DateFormatTool.DATETIME_FORMAT, new Date()));
            int rec = entitymDao.insertRecords(entitym);
            log.debug("新增数据返回 {}",rec);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            log.error("新增数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("新增数据出错：" + e.getMessage());
        }
        return result;

    }

    /**
     * 修改
     *
     * @param entitym
     * @param requestPojo
     * @return Result.class
     */
    @Override
    public Result doUpdate(SYLOG entitym, RequestPojo requestPojo) {
        log.debug("SYLOGSvcImpl.doUpdate parameter: {}", entitym);
        Result result = new Result();
        //非空验证
        if(StringUtils.isEmpty(entitym.getCompanyID()) || StringUtils.isEmpty(entitym.getSPINFO())) {
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG);
            return result;
        }

        try{
            int rec = entitymDao.updateRecords(entitym);
            log.debug("更新数据返回 {}",rec);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            log.error("修改数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("修改数据出错：" + e.getMessage());
        }
        return result;

    }

    /**
     * 删除
     *
     * @param map
     * @return Result.class
     * */
    @Override
    public Result doDelete(Map<String, String> map,RequestPojo requestPojo) {
        Result result = new Result();
        log.debug("SYLOGSvcImpl.doDelete parameter: {}", map);
        //非空验证
        String CompanyID = ParamUtil.getParamfromMap(map, "CompanyID");
        String SPINFO = ParamUtil.getParamfromMap(map, "SPINFO");

        if(StringUtils.isEmpty(CompanyID) || StringUtils.isEmpty(SPINFO)){
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG);
            return result;
        }

        Page page = new Page();
        Map<String, String> mapData = new HashMap<>();
        mapData.put("CompanyID", CompanyID);
        mapData.put("SPINFO", SPINFO);

        List<SYLOG> entitym = entitymDao.selectList(page, mapData);
        if(entitym == null || entitym.size() == 0){
            result.setResStatus(ResStatus.STATUS_UNAVAILABLE);
            result.setResMsg("数据不存在");
            return result;
        }

        try{
            int rec = entitymDao.deleteByPk(mapData);
            log.debug("删除数据返回 {}",rec);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            log.error("删除数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("删除出错：" + e.getMessage());
        }

        return result;

    }

    @Override
    public Result doSaveMap(String map, RequestPojo requestPojo) {
        log.info("doSaveMap {}", map);
        JSONObject json = JSON.parseObject(map);
        JSONObject data = json.getJSONObject("data");
        SYLOG entitym = JSonUtil.toBean(data.getJSONObject("param").toJSONString(), SYLOG.class);
        requestPojo.setStaffId(ParamUtil.getMetadatafromObject(map, "user"));

        return this.doSave(entitym, requestPojo);
    }

    @Override
    public Result doUpdateMap(String map, RequestPojo requestPojo) {
        log.info("doUpdateMap {}", map);
        JSONObject json = JSON.parseObject(map);
        JSONObject data = json.getJSONObject("data");
        SYLOG entitym = JSonUtil.toBean(data.getJSONObject("param").toJSONString(), SYLOG.class);
        requestPojo.setStaffId(ParamUtil.getMetadatafromObject(map, "user"));

        return this.doUpdate(entitym, requestPojo);
    }
}
