package com.hfkj.service.quiz.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsOilPriceQuizUserMapper;
import com.hfkj.entity.*;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.service.CommonService;
import com.hfkj.service.quiz.BsOilPriceQuizUserService;
import com.hfkj.service.user.BsUserService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsOilPriceQuizUserServiceImpl
 * @author: HuRui
 * @date: 2025/11/24
 **/
@Service("oilPriceQuizUserService")
public class BsOilPriceQuizUserServiceImpl implements BsOilPriceQuizUserService {
    @Resource
    private BsOilPriceQuizUserMapper oilPriceQuizUserMapper;
    @Resource
    private BsOilPriceAdjustDateService oilPriceAdjustDateService;
    @Resource
    private BsUserService userService;
    @Resource
    private CommonService commonService;

    @Override
    public void edit(BsOilPriceQuizUser data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setStatus(1);
            oilPriceQuizUserMapper.insert(data);
        }
        oilPriceQuizUserMapper.updateByPrimaryKey(data);
    }

    @Override
    public void quiz(Date priceAdjustDate, Long cityCode, Long userId, OilPriceAdjustDateResultEnum quizOption) {
        // 调价日
        BsOilPriceAdjustDate adjustDate = oilPriceAdjustDateService.getDetailByDate(priceAdjustDate);
        if (adjustDate == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到调价日数据");
        }
        // 用户
        BsUser user = userService.getUser(userId);
        if (user == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户数据");
        }
        // 城市
        SecRegion city = commonService.getRegionsById(cityCode);
        if (city == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到地区数据");
        }
        // 城市
        SecRegion province = commonService.getRegionsById(city.getParentId());
        if (province == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到地区数据");
        }
        BsOilPriceQuizUser quiz = new BsOilPriceQuizUser();
        quiz.setPriceAdjustDateId(adjustDate.getId());
        quiz.setPriceAdjustDate(adjustDate.getDate());
        quiz.setProvinceCode(province.getRegionId());
        quiz.setProvinceName(province.getRegionName());
        quiz.setCityCode(city.getRegionId());
        quiz.setCityName(city.getRegionName());
        quiz.setUserId(user.getId());
        quiz.setUserPhone(user.getPhone());
        quiz.setQuizOption(quizOption.getType());
        quiz.setStatus(1);
        edit(quiz);
    }

    @Override
    public BsOilPriceQuizUser getDetail(Date priceAdjustDate, Long userId) {
        BsOilPriceQuizUserExample example = new BsOilPriceQuizUserExample();
        example.createCriteria()
                .andStatusNotEqualTo(0)
                .andPriceAdjustDateEqualTo(priceAdjustDate)
                .andUserIdEqualTo(userId);
        example.setOrderByClause("create_time desc");
        List<BsOilPriceQuizUser> list = oilPriceQuizUserMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsOilPriceQuizUser getDetail(Long id) {
        return oilPriceQuizUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsOilPriceQuizUser> getList(Map<String, Object> param) {
        BsOilPriceQuizUserExample example = new BsOilPriceQuizUserExample();
        BsOilPriceQuizUserExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getLong(param, "priceAdjustDateId") != null) {
            criteria.andPriceAdjustDateIdEqualTo(MapUtils.getLong(param, "priceAdjustDateId"));
        }
        if (MapUtils.getLong(param, "priceAdjustDate") != null) {
            criteria.andPriceAdjustDateEqualTo(new Date(MapUtils.getLong(param, "priceAdjustDate")));
        }
        if (MapUtils.getLong(param, "provinceCode") != null) {
            criteria.andProvinceCodeEqualTo(MapUtils.getLong(param, "provinceCode"));
        }
        if (MapUtils.getLong(param, "cityCode") != null) {
            criteria.andCityCodeEqualTo(MapUtils.getLong(param, "cityCode"));
        }
        if (MapUtils.getLong(param, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(param, "userId"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(param, "userPhone"))) {
            criteria.andUserPhoneLike("%"+MapUtils.getString(param, "userPhone")+"%");
        }
        if (MapUtils.getInteger(param, "quizOption") != null) {
            criteria.andQuizOptionEqualTo(MapUtils.getInteger(param, "quizOption"));
        }
        if (MapUtils.getBoolean(param, "quizResult") != null) {
            criteria.andQuizResultEqualTo(MapUtils.getBoolean(param, "quizResult"));
        }
        if (MapUtils.getBoolean(param, "noticeStatus") != null) {
            criteria.andNoticeStatusEqualTo(MapUtils.getBoolean(param, "noticeS x`tatus"));
        }
        example.setOrderByClause("create_time desc");
        return oilPriceQuizUserMapper.selectByExample(example);
    }
}
