package io.hmit.modules.serviceOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.serviceOrder.dao.PensionConsigneeDao;
import io.hmit.modules.serviceOrder.dto.PensionConsigneeAddDTO;
import io.hmit.modules.serviceOrder.dto.PensionConsigneeDTO;
import io.hmit.modules.serviceOrder.dto.PensionOldFamilyDTO;
import io.hmit.modules.serviceOrder.entity.PensionConsigneeEntity;
import io.hmit.modules.serviceOrder.service.PensionConsigneeService;
import io.hmit.modules.serviceOrder.service.PensionOldFamilyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-05-06
 */
@Service
public class PensionConsigneeServiceImpl extends CrudServiceImpl<PensionConsigneeDao, PensionConsigneeEntity, PensionConsigneeDTO> implements PensionConsigneeService {

    @Override
    public QueryWrapper<PensionConsigneeEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PensionConsigneeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private PensionConsigneeDao pensionConsigneeDao;

    @Autowired
    private PensionOldFamilyService pensionOldFamilyService;

    @Override
    public List<PensionConsigneeDTO> findByUserId(Long userId) {
        QueryWrapper<PensionConsigneeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<PensionConsigneeEntity> platformPayInfoEntityList = pensionConsigneeDao.selectList(queryWrapper);
        return ConvertUtils.sourceToTarget(platformPayInfoEntityList, PensionConsigneeDTO.class);
    }

    @Override
    public List<PensionConsigneeDTO> findMyFamilyAddress(Long userId) {
        //查询个人及家庭的userId
        List<PensionOldFamilyDTO> familyDTOS = pensionOldFamilyService.findByOldId(userId);
        List<PensionConsigneeDTO> userAddressList = findByUserId(userId);
        for (PensionOldFamilyDTO pensionOldFamilyDTO : familyDTOS) {
            List<PensionConsigneeDTO> address = findByUserId(pensionOldFamilyDTO.getFamilyMemberId());
            if (address.size() >= 1) {
                userAddressList.addAll(address);
            }
        }
        return userAddressList;
    }

    @Override
    public void addUserAddress(Map<String, Object> params) {
        Integer ifDefault = 1;
        QueryWrapper<PensionConsigneeEntity> queryWrapper = new QueryWrapper<>();
        Long userId = Long.parseLong(params.get("userId").toString());
        queryWrapper.eq("user_id", userId).eq("if_default", 1);
        PensionConsigneeEntity pensionConsigneeEntity = pensionConsigneeDao.selectOne(queryWrapper);
        if (pensionConsigneeEntity != null) {
            if (Integer.parseInt(params.get("ifDefault").toString()) == 0) { //不希望设置为默认地址
                ifDefault = 0;
            } else { //设为默认地址并将其他地址设置为非默认
                pensionConsigneeEntity.setIfDefault(0);
                update(ConvertUtils.sourceToTarget(pensionConsigneeEntity, PensionConsigneeDTO.class));
            }
        }

        PensionConsigneeAddDTO addressDTO = new PensionConsigneeAddDTO(userId, params.get("username").toString()
                ,params.get("mobile").toString(), params.get("province").toString(), params.get("city").toString(), params.get("district").toString(),
                params.get("address").toString(), params.get("zip").toString(), ifDefault, 1);
        PensionConsigneeDTO pensionConsigneeDTO = new PensionConsigneeDTO();
        BeanUtils.copyProperties(addressDTO, pensionConsigneeDTO);
        pensionConsigneeDTO.setCreateDate(new Date());
        save(pensionConsigneeDTO);
    }

    @Override
    public void addUserAddress(PensionConsigneeAddDTO dto) {
        Integer ifDefault = 1;
        QueryWrapper<PensionConsigneeEntity> queryWrapper = new QueryWrapper<>();
        Long userId = dto.getUserId();
        queryWrapper.eq("user_id", userId).eq("if_default", 1);
        PensionConsigneeEntity pensionConsigneeEntity = pensionConsigneeDao.selectOne(queryWrapper);
        if (pensionConsigneeEntity != null) {
            if (dto.getIfDefault() == 0) { //不希望设置为默认地址
                ifDefault = 0;
            } else { //设为默认地址并将其他地址设置为非默认
                pensionConsigneeEntity.setIfDefault(0);
                update(ConvertUtils.sourceToTarget(pensionConsigneeEntity, PensionConsigneeDTO.class));
            }
        }

        if (null != dto.getId()) {
            PensionConsigneeDTO pensionConsigneeDTO = get(dto.getId());
            BeanUtils.copyProperties(dto, pensionConsigneeDTO);
            pensionConsigneeDTO.setIfDefault(ifDefault);
            update(pensionConsigneeDTO);
        } else {
            PensionConsigneeDTO pensionConsigneeDTO = new PensionConsigneeDTO();
            BeanUtils.copyProperties(dto, pensionConsigneeDTO);
            pensionConsigneeDTO.setCreateDate(new Date());
            pensionConsigneeDTO.setIfDefault(ifDefault);
            pensionConsigneeDTO.setStatus(1);
            save(pensionConsigneeDTO);
        }

    }
}