package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicRoadShowAudit;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowStage;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowState;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.RoadshowMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.RoadShowSearchModel;
import com.lightinit.hsdataplatform.service.admin.IRoadShowAuditService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class RoadShowAuditServiceImpl implements IRoadShowAuditService {

    @Autowired
    private RoadshowMapper roadshowMapper;
    @Transactional
    @Override
   public ResultState doEdit(RoadshowWithBLOBs inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowExample example=new RoadshowExample();
        RoadshowExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(inputModel.getId());
        long sqlResult = roadshowMapper.updateByExampleSelective(inputModel,example);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Override
    public List<Roadshow> queryList(RoadShowSearchModel inputModel, BaseExample.Page page) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowExample example = new RoadshowExample();
        if (inputModel != null) {
            RoadshowExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(inputModel.getRoadShowName())) {
                criteria.andRoadshowNameLike("%" + inputModel.getRoadShowName() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowStage())) {
                if (!inputModel.getRoadShowStage().equals(DicRoadShowStage.ALL)) {
                    criteria.andProjectPhaseEqualTo(inputModel.getRoadShowStage());
                }
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowState())) {
                criteria.andRoadshowStateEqualTo(inputModel.getRoadShowState());
            }
            if (inputModel.getEnrollTime() !=null) {
                criteria.andPublishAtGreaterThanOrEqualTo(inputModel.getEnrollTime());
            }
        }
        example.setPage(page);
        return roadshowMapper.selectByExample(example);
    }

    @Override
    public long queryCount(RoadShowSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowExample example = new RoadshowExample();
        if (inputModel != null) {
            RoadshowExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(inputModel.getRoadShowName())) {
                criteria.andRoadshowNameLike("%" + inputModel.getRoadShowName() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowStage())) {
                if (!inputModel.getRoadShowStage().equals(DicRoadShowStage.ALL)) {//全部
                    criteria.andProjectPhaseEqualTo(inputModel.getRoadShowStage());
                }
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowState())) {
                criteria.andRoadshowStateEqualTo(inputModel.getRoadShowState());
            }
            if (inputModel.getEnrollTime() !=null) {
                criteria.andPublishAtGreaterThanOrEqualTo(inputModel.getEnrollTime());
            }
        }
        return roadshowMapper.countByExample(example);
    }
}
