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

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.base.log.service.TSysLogSvc;
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.sydept.model.SYDepVer;
import com.fufan.mtsaas.modules.sysmgr.sydept.service.SYDepVerSvc;
import com.github.pagehelper.ISelect;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptVerQueryRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptVerSaveRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.dao.SYDepVerDao;
import com.fufan.mtsaas.modules.sysmgr.sydept.model.SYDepVer;
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.sydept.service.SYDepVerSvc;
import com.fufan.mtsaas.util.DateFormatTool;
import com.fufan.mtsaas.util.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Liby
 * @since 2019-08-19
 */
@Service("SYDepVerSvcImpl")
@Slf4j
@Primary
public class SYDepVerSvcImpl extends ServiceImpl<SYDepVerDao, SYDepVer> implements SYDepVerSvc {
    @Autowired
    private SYDepVerDao entitymDao;

    @Value("${COMPANY_CD}")
    private String COMPANY_CD;

    @Autowired
    private TSysLogSvc tSysLogSvc;

    /**
     * 查询
     *
     * @param map
     * @return Result.class
     * */
    @Override
    public Result doQueryPage(DeptVerQueryRequest map) {
        Result result = new Result();
        //分页查询
        Page page = new Page();
        log.debug("SYDepVerSvcImpl.doQueryPage parameter: {}", map);
        Map<String, String> mapData = new HashMap<>();
        String CompanyID = map.getCompanyID();
        String VerNo = map.getVerNo();
        String EffDt = map.getEffDt();
        String DepName = map.getDepName();

        //赋值默认
        if(StringUtils.isEmpty(CompanyID)){
            CompanyID = COMPANY_CD;
        }

        mapData.put("CompanyID", CompanyID);
        mapData.put("VerNo", VerNo);
        mapData.put("EffDt", EffDt);

        List<SYDepVer> entitys;

        //分页处理 -- 20192021 Liby
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                entitymDao.selectList(page, mapData);
            }
        });
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        entitys = entitymDao.selectList(page, mapData);

        log.debug("SYDepVerSvcImpl.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
    // @Transactional
    public Result doSave(SYDepVer entitym, RequestPojo requestPojo) {
        log.debug("SYDepVerSvcImpl.doSave parameter: {}", entitym);
        Result result = new Result();
        //非空验证
        if(StringUtils.isEmpty(entitym.getCompanyID()) || StringUtils.isEmpty(entitym.getEffDt()) || StringUtils.isEmpty(entitym.getVerNo())){
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG);
            return result;
        }

        //名称重复验证
        HashMap mapData = new HashMap();
        Page page = new Page();
        mapData.put("CompanyID", entitym.getCompanyID());
        mapData.put("VerNo", entitym.getVerNo());

        List<SYDepVer> entity = entitymDao.selectList(page, mapData);
        if(entity != null && entity.size() > 0){
            result.setResStatus(ResStatus.STATUS_EXISTENCE);
            result.setResMsg(ResStatus.STATUS_EXISTENCE_MSG);
            return result;
        }

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

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "version", "新增版本", "版本名称:" + entitym.getVerNo(), "新增", "成功", requestPojo);

            result.setResData(page.setRecords(Arrays.asList(entitym)));
        }catch (Exception e){
            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "version", "新增版本", "版本名称:" + entitym.getVerNo(), "新增", "失败", requestPojo);

            log.error("新增数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("新增数据出错：" + e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus()().setRollbackOnly();
        }
        return result;

    }

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

        entitym.setUpdatedPerson(requestPojo.getUserName());
        entitym.setUpdatedDT(DateFormatTool.dateToString(DateFormatTool.DATETIME_FORMAT, new Date()));
        try{
            int rec = entitymDao.updateRecords(entitym);
            log.debug("更新数据返回 {}",rec);

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "version", "修改版本", "版本名称:" + entitym.getVerNo(), "更新", "成功", requestPojo);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "version", "修改版本", "版本名称:" + entitym.getVerNo(), "更新", "失败", requestPojo);

            log.error("修改数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("修改数据出错：" + e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus()().setRollbackOnly();
        }
        return result;

    }

    /**
     * 删除
     *
     * @param map
     * @return Result.class
     * */
    @Override
    // @Transactional
    public Result doDelete(DeptVerQueryRequest map, RequestPojo requestPojo) {
        Result result = new Result();
        log.debug("SYDepVerSvcImpl.doDelete parameter: {}", map);
        //非空验证
        String CompanyID = map.getCompanyID();
        String EffDt = map.getEffDt();
        String VerNo = map.getVerNo();

        if(StringUtils.isEmpty(CompanyID) || StringUtils.isEmpty(VerNo) || StringUtils.isEmpty(EffDt)){
            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("VerNo", VerNo);
        mapData.put("EffDt", EffDt);
        List<SYDepVer> entitym = entitymDao.selectList(page, mapData);
        if(entitym == null || entitym.size() == 0){
            result.setResStatus(ResStatus.STATUS_UNAVAILABLE);
            result.setResMsg(ResStatus.STATUS_UNAVAILABLE_MSG);
            return result;
        }

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

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.get(0).getId()), "version", "删除版本", "版本名称:" + entitym.get(0).getVerNo(), "删除", "成功", requestPojo);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.get(0).getId()), "version", "删除版本", "版本名称:" + entitym.get(0).getVerNo(), "删除", "失败", requestPojo);

            log.error("删除数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("删除出错：" + e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus()().setRollbackOnly();
        }

        return result;

    }

    @Override
    public Result doSaveMap(DeptVerSaveRequest map, RequestPojo requestPojo) {
        log.info("doSaveMap {}", map);
        SYDepVer entitym = new SYDepVer();
        BeanUtils.copyProperties(map, entitym);

        return this.doSave(entitym, requestPojo);
    }

    @Override
    public Result doUpdateMap(DeptVerSaveRequest map, RequestPojo requestPojo) {
        log.info("doUpdateMap {}", map);
        SYDepVer entitym = new SYDepVer();
        BeanUtils.copyProperties(map, entitym);

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