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.AdminMapper;
import com.lightinit.hsdataplatform.mapper.RoadshowMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.ActivitySearchModel;
import com.lightinit.hsdataplatform.model.admin.RoadShowSearchModel;
import com.lightinit.hsdataplatform.service.admin.IRoadShowMgrService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class RoadShowMgrServiceImpl implements IRoadShowMgrService {

    @Autowired
    private RoadshowMapper roadshowMapper;
    @Autowired
    private AdminMapper adminMapper;

    @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())) {
                if(inputModel.getRoadShowState().equals(DicRoadShowState.PENDING)){
                    criteria.andAuditResultIsNull();
                }else if(inputModel.getRoadShowState().equals(DicRoadShowState.REJECTED)){
                    criteria.andAuditResultEqualTo(DicRoadShowState.REJECTED);
                }else{
                    criteria.andAuditResultEqualTo(DicRoadShowState.APPROVED);
                }
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowTrade())) {
                if (!inputModel.getRoadShowTrade().equals(DicRoadShowStage.ALL)) {
                    criteria.andTradeFieldEqualTo(inputModel.getRoadShowTrade());
                }
            }
            if (inputModel.getEnrollTime() !=null) {
                criteria.andPublishAtGreaterThanOrEqualTo(inputModel.getEnrollTime());
            }
        }
        example.setPage(page);
        return roadshowMapper.selectByExample(example);
    }

    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())) {

                if(inputModel.getRoadShowState().equals(DicRoadShowState.PENDING)){
                    criteria.andAuditResultIsNull();
                }else if(inputModel.getRoadShowState().equals(DicRoadShowState.REJECTED)){
                    criteria.andAuditResultEqualTo(DicRoadShowState.REJECTED);
                }else{
                    criteria.andAuditResultEqualTo(DicRoadShowState.APPROVED);
                }
            }
            if (StringUtils.isNotEmpty(inputModel.getRoadShowTrade())) {
                if (!inputModel.getRoadShowTrade().equals(DicRoadShowStage.ALL)) {
                    criteria.andTradeFieldEqualTo(inputModel.getRoadShowTrade());
                }
            }
            if (inputModel.getEnrollTime() !=null) {
                criteria.andPublishAtGreaterThanOrEqualTo(inputModel.getEnrollTime());
            }
        }
        return roadshowMapper.countByExample(example);
    }

    @Override
    public ResultState<Long> delete(long id) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowExample example = new RoadshowExample();
        RoadshowExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        if (!(roadshowMapper.deleteByExample(example)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }

    @Override
    public Map<String, Object> queryOne(long id) {
        RoadshowWithBLOBs roadshowWithBLOBs= roadshowMapper.selectByPrimaryKey(id);
        Map<String, Object> map = new HashMap<>();
        map.put("roadshowWithBLOBs",roadshowWithBLOBs);
        if (roadshowWithBLOBs.getAuditedBy() != null){
            AdminExample adminExample = new AdminExample();
            adminExample.createCriteria().andIdEqualTo(roadshowWithBLOBs.getAuditedBy());
            List<Admin> admins = adminMapper.selectByExample(adminExample);
            map.put("auditor",admins.get(0).getUsername());
        }
        return map;
    }
}
