package com.mvc.service.log;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.OperationType;
import com.mvc.dao.OperationLogMapper;
import com.mvc.entity.OperationLog;
import com.mvc.entity.OperationLogExample;
import com.mvc.model.*;
import com.mvc.model.log.OperationLogModel;
import com.mvc.service.TAffiliatedunitService;
import com.mvc.utils.DateUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OperationLogService {

    private static Logger log = LoggerFactory.getLogger(OperationLogService.class);

    @Resource
    private OperationLogMapper operationLogMapper;

    @Resource
    private TAffiliatedunitService tAffiliatedunitService;

    /**
     * 分页查询全部记录
     *
     * @param queryModel
     * @return
     */
    public ResPageModel findAllOperationLog(OperationLogModel queryModel) {
        OperationLogExample example = new OperationLogExample();
        OperationLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getOperationMan())) {
            criteria.andOperationManLike(StringUtil.getLikeFormat(queryModel.getOperationMan()));
        }
        if (queryModel.getOperationType() != null) {
            criteria.andOperationTypeEqualTo(queryModel.getOperationType());
        }
        if (queryModel.getUserType() != null) {
            criteria.andUserTypeEqualTo(queryModel.getUserType());
        }
        if (StringUtils.isNotEmpty(queryModel.getUnitCode())) {
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }
        if (queryModel.getQueryTimeRange() != null && queryModel.getQueryTimeRange().length == 2) {
            criteria.andOperationTimeGreaterThanOrEqualTo(DateUtil.parseTimeStr(queryModel.getQueryTimeRange()[0], DateUtil.PATTERN_DATA_NORMAL));
            criteria.andOperationTimeLessThanOrEqualTo(DateUtil.parseTimeStr(queryModel.getQueryTimeRange()[1], DateUtil.PATTERN_DATA_NORMAL));
        }
        example.setOrderByClause("OPERATION_TIME desc");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<OperationLog> operationLogs = operationLogMapper.selectByExample(example);
        List<OperationLogModel> resList = new ArrayList<>();
        for (OperationLog operationLog : operationLogs) {
            OperationLogModel model = new OperationLogModel();
            BeanUtils.copyProperties(operationLog, model);
            resList.add(model);
        }
        ResPageModel resPageModel = new ResPageModel(resList, pageObject);
        return resPageModel;
    }

    /**
     * 保存新增对象
     *
     * @param model
     * @return
     */
    public OperationLogModel saveOperationLog(OperationLogModel model) {
        model.setOperationTime(new Date());

        OperationLog operationLog = new OperationLog();
        BeanUtils.copyProperties(model, operationLog);
        int insert = operationLogMapper.insert(operationLog);
        model.setId(operationLog.getId());

        return model;
    }

    /**
     * 根据ID获取升级计划对象
     *
     * @param id
     * @return
     */
    public OperationLogModel findOperationLogModelById(int id) {
        OperationLog plan = operationLogMapper.selectByPrimaryKey(id);
        if (plan != null) {
            OperationLogModel model = new OperationLogModel();
            BeanUtils.copyProperties(plan, model);
            return model;
        }
        return null;
    }

    /**
     * 保存操作日志
     *
     * @param userType
     * @param operationType
     * @param userModel
     * @param content
     * @return
     */
    public OperationLogModel saveOperationLog(int userType, OperationType operationType, Object userModel, String content) {
        try {
            OperationLogModel model = new OperationLogModel();
            model.setUserType(userType);
            model.setOperationType(operationType.getFlag());
            model.setOperationContent(StringUtils.isEmpty(content) ? operationType.getDesc() : content);

            if (userType == 0) {//内部用户
                FacUserModel facUserModel = (FacUserModel) userModel;
                model.setUserId(facUserModel.getId());
                model.setOperationMan(facUserModel.getUserName());
            } else if (userType== 1){//客户
                CustomUserModel customUserModel = (CustomUserModel) userModel;
                model.setUserId(customUserModel.getId());
                model.setOperationMan(customUserModel.getUserName());
                model.setUnitCode(customUserModel.getUnitCode());
                if(StringUtils.isEmpty(customUserModel.getUnitName())){
                    TAffiliatedunitModel affiliatedunitModel = tAffiliatedunitService.findTAffiliatedunit(customUserModel.getUnitCode());
                    if(affiliatedunitModel!=null){
                        model.setUnitName(affiliatedunitModel.getUnitName());
                    }
                }else {
                    model.setUnitName(customUserModel.getUnitName());
                }
            }else {//APP
                AppUserModel appUserModel = (AppUserModel) userModel;
                model.setUserId(appUserModel.getId());
                model.setOperationMan(appUserModel.getUserName());
                model.setUnitCode(appUserModel.getUnitCode());
                model.setUnitName(appUserModel.getUnitName());
            }
            return saveOperationLog(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
        }
        return null;
    }
}
