package com.ycwl.basic.service.impl.pc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.ScenicAccountMapper;
import com.ycwl.basic.mapper.ScenicMapper;
import com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicAddOrUpdateReq;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.service.task.TaskFaceService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 15:25
 */
@Slf4j
@Service
public class ScenicServiceImpl implements ScenicService {
    @Autowired
    private ScenicMapper scenicMapper;
    @Autowired
    private ScenicAccountMapper scenicAccountMapper;
    @Autowired
    private TaskFaceService taskFaceService;
    @Autowired
    private ScenicRepository scenicRepository;

    @Override
    public ApiResponse<PageInfo<ScenicRespVO>> pageQuery(ScenicReqQuery scenicReqQuery) {
        PageHelper.startPage(scenicReqQuery.getPageNum(), scenicReqQuery.getPageSize());
        List<ScenicRespVO> list = scenicMapper.list(scenicReqQuery);
        PageInfo<ScenicRespVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<ScenicRespVO>> list(ScenicReqQuery scenicReqQuery) {
        return ApiResponse.success(scenicMapper.list(scenicReqQuery));
    }

    @Override
    public ApiResponse<ScenicRespVO> getById(Long id) {
        return ApiResponse.success(scenicMapper.getById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResponse<Boolean> add(ScenicAddOrUpdateReq scenicAddReq) {
        ScenicAccountEntity scenicAccount = scenicAccountMapper.getByAccount(scenicAddReq.getAccount());
        if (scenicAccount != null) {
            return ApiResponse.fail("账号已存在");
        }
        Long scenicId = SnowFlakeUtil.getLongId();
        scenicAddReq.setId(scenicId);
        int add = scenicMapper.add(scenicAddReq);
        ScenicAccountEntity account = new ScenicAccountEntity();
        account.setId(SnowFlakeUtil.getLongId());
        account.setScenicId(scenicId);
        account.setName(scenicAddReq.getName() + "管理员");
        account.setAccount(scenicAddReq.getAccount());
        account.setPassword(scenicAddReq.getPassword());
        account.setIsSuper(1);
        scenicAccountMapper.add(account);
        if (add > 0) {
            taskFaceService.createFaceDB(scenicId.toString());
            taskFaceService.assureFaceDB(scenicId.toString());
            return ApiResponse.success(true);
        } else {
            return ApiResponse.fail("景区添加失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResponse<Boolean> deleteById(Long id) {
        int i = scenicMapper.deleteById(id);
        if (i > 0) {
            scenicMapper.deleteConfigByScenicId(id);
            scenicAccountMapper.deleteByScenicId(id);
            taskFaceService.deleteFaceDB(id.toString());
            scenicRepository.clearCache(id);
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("景区删除失败");
        }
    }

    @Override
    public ApiResponse<Boolean> update(ScenicAddOrUpdateReq scenicUpdateReq) {
        if (StringUtils.isNotBlank(scenicUpdateReq.getAccount()) && StringUtils.isNotBlank(scenicUpdateReq.getPassword())) {
            ScenicAccountEntity scenicAccount = scenicAccountMapper.getByAccount(scenicUpdateReq.getAccount());
            if (scenicAccount != null) {
                if (!scenicAccount.getScenicId().equals(scenicUpdateReq.getId())) {
                    return ApiResponse.fail("账号已存在");
                }
            }
            ScenicAccountEntity account = scenicAccountMapper.getSuperAccountOfScenic(scenicUpdateReq.getId());
            if (account != null) {
                account.setAccount(scenicUpdateReq.getAccount());
                account.setPassword(scenicUpdateReq.getPassword());
                scenicAccountMapper.update(account);
            } else {
                account = new ScenicAccountEntity();
                account.setId(SnowFlakeUtil.getLongId());
                account.setScenicId(scenicUpdateReq.getId());
                account.setName(scenicUpdateReq.getName() + "管理员");
                account.setAccount(scenicUpdateReq.getAccount());
                account.setPassword(scenicUpdateReq.getPassword());
                account.setIsSuper(1);
                scenicAccountMapper.add(account);
            }
        }
        int i = scenicMapper.update(scenicUpdateReq);
        if (i > 0) {
            scenicRepository.clearCache(scenicUpdateReq.getId());
            taskFaceService.assureFaceDB(scenicUpdateReq.getId().toString());
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("景区修改失败");
        }

    }

    @Override
    public ApiResponse<Boolean> updateStatus(Long id) {
        int i = scenicMapper.updateStatus(id);
        if (i > 0) {
            scenicRepository.clearCache(id);
            taskFaceService.assureFaceDB(id.toString());
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("景区状态修改失败");
        }
    }

    @Override
    public ApiResponse<Boolean> addConfig(ScenicConfigEntity scenicConfig) {
        if (scenicConfig.getId() == null) {
            scenicConfig.setId(SnowFlakeUtil.getLongId());
        }
        int i = scenicMapper.addConfig(scenicConfig);
        if (i > 0) {
            scenicRepository.clearCache(scenicConfig.getScenicId());
            taskFaceService.assureFaceDB(scenicConfig.getScenicId().toString());
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("景区配置添加失败");
        }
    }

    @Override
    public ApiResponse<Boolean> updateConfigById(ScenicConfigEntity scenicConfig) {
        int i = scenicMapper.updateConfigById(scenicConfig);
        if (i > 0) {
            scenicRepository.clearCache(scenicConfig.getScenicId());
            taskFaceService.assureFaceDB(scenicConfig.getScenicId().toString());
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("景区配置修改失败");
        }
    }

    @Override
    public ScenicConfigEntity getConfig(Long id) {
        return scenicRepository.getScenicConfig(id);
    }

    @Override
    public void saveConfig(Long configId, ScenicConfigEntity config) {
        config.setId(configId);
        if (config.getScenicId() == null) {
            throw new RuntimeException("景区ID不能为空");
        }
        scenicMapper.updateConfigById(config);
        scenicRepository.clearCache(config.getScenicId());
    }
}
