package com.njyunzhi.pet_identity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njyunzhi.pet_identity.common.Constants;
import com.njyunzhi.pet_identity.common.DateUtils;
import com.njyunzhi.pet_identity.entity.SysSetting;
import com.njyunzhi.pet_identity.entity.TaApplication;
import com.njyunzhi.pet_identity.entity.TaPetIdentity;
import com.njyunzhi.pet_identity.mapper.SysSettingMapper;
import com.njyunzhi.pet_identity.mapper.TaPetIdentityMapper;
import com.njyunzhi.pet_identity.service.ITaPetIdentityService;
import com.njyunzhi.pet_identity.vo.MakeCardParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * <p>
 * 宠物证件 服务实现类
 * </p>
 *
 * @author yansen
 * @since 2022-05-25
 */
@Service
public class TaPetIdentityServiceImpl extends BaseServiceImpl<TaPetIdentityMapper, TaPetIdentity> implements ITaPetIdentityService {

    @Autowired
    SysSettingMapper sysSettingMapper;

    @Override
    public TaPetIdentity createNewCard(TaApplication taApplication, MakeCardParam makeCardParam) throws Exception {

        SysSetting sysSetting = sysSettingMapper.selectById(Constants.SYSSETTING_CARD_EXPIRE);

        // 北京时间
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        LocalDateTime expire = now.plusDays(Long.parseLong(sysSetting.getContent()));

        TaPetIdentity taPetIdentity = new TaPetIdentity();
        taPetIdentity.setCardNo(makeCardParam.getCardNo());
        taPetIdentity.setPersonId(taApplication.getPersonId());
        taPetIdentity.setPersonName(taApplication.getPersonName());
        taPetIdentity.setPhone(taApplication.getPhone());
        taPetIdentity.setPetId(taApplication.getPetId());
        taPetIdentity.setPetName(taApplication.getPetName());
        taPetIdentity.setPetSex(taApplication.getPetSex());
        taPetIdentity.setPetColor(taApplication.getPetColor());
        taPetIdentity.setPetType(taApplication.getPetType());
        taPetIdentity.setPetImg1(taApplication.getImg1());
        taPetIdentity.setPetImg2(taApplication.getImg2());
        taPetIdentity.setAddress(taApplication.getAddress());
        taPetIdentity.setStartDate(DateUtils.toString(now, "yyyy-MM-dd"));
        taPetIdentity.setExpireDate(DateUtils.toString(expire, "yyyy-MM-dd"));
        taPetIdentity.setStatus(Constants.STATUS_NORMAL);
        taPetIdentity.setCreateDate(now);

        taPetIdentity.setIsOrg(taApplication.getIsOrg());
        taPetIdentity.setOrgId(taApplication.getOrgId());
        taPetIdentity.setOrgName(taApplication.getOrgName());
        taPetIdentity.setOrgLicense(taApplication.getOrgLicense());
        taPetIdentity.setCriminalInfo(taApplication.getCriminalInfo());
        taPetIdentity.setPenaltyInfo(taApplication.getPenaltyInfo());

        if (!save(taPetIdentity)) {
            throw new Exception("生成证件失败");
        }

        return taPetIdentity;
    }

    @Override
    public boolean extendDate(TaPetIdentity taPetIdentity) throws Exception {

        SysSetting sysSetting = sysSettingMapper.selectById(Constants.SYSSETTING_CARD_EXPIRE);

        // 北京时间
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        LocalDateTime expire = now.plusDays(Long.parseLong(sysSetting.getContent()));

        taPetIdentity.setStartDate(DateUtils.toString(now, "yyyy-MM-dd"));
        taPetIdentity.setExpireDate(DateUtils.toString(expire, "yyyy-MM-dd"));

        return updateById(taPetIdentity);
    }

    @Override
    public TaPetIdentity getByPet(String petId, String personId) {
        QueryWrapper<TaPetIdentity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id", petId);
        queryWrapper.eq("person_id", personId);
        queryWrapper.in("status", new Integer[] { Constants.CARD_STATUS_NORMAL, Constants.CARD_STATUS_EXPIRE });
        queryWrapper.last("limit 1");

        return getOne(queryWrapper);
    }

    @Override
    public int countNotDelete() {
        return baseMapper.countNotDelete();
    }
}
