package com.dayou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dayou.common.BaseEntity;
import com.dayou.dto.TaskRecordDTO;
import com.dayou.entity.Personal;
import com.dayou.entity.PersonalTarget;
import com.dayou.enums.CertificateEnum;
import com.dayou.mapper.PersonalTargetMapper;
import com.dayou.service.IPersonalService;
import com.dayou.service.IPersonalTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Struct;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

import static com.dayou.enums.CertificateEnum.*;

/**
 * <p>
 * 个贷估价对象 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-03-01
 */
@DS("mbs")
@Service
public class PersonalTargetServiceImpl extends ServiceImpl<PersonalTargetMapper, PersonalTarget> implements IPersonalTargetService {

    @Autowired
    private IPersonalService personalService;
    @Autowired
    private PersonalTargetMapper personalTargetMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<PersonalTarget> selectPage(Page page,PersonalTarget personalTarget){
        return this.page(page, new QueryWrapper<PersonalTarget>(personalTarget));
    }


    @Override
    public PersonalTarget detail(Long id){
        return this.getOne(new LambdaQueryWrapper<PersonalTarget>().eq(PersonalTarget::getPersonalId,id).eq(BaseEntity::getDeleted,Boolean.FALSE));
    }

    @Override
    public Boolean add(PersonalTarget personalTarget){
        return  this.save(personalTarget);
    }

    @Override
    public Boolean updateFeedback(PersonalTarget personalTarget){
        String feedback = personalTarget.getFeedback();
        if (StrUtil.isNotEmpty(feedback)){
 //           PersonalTarget target = this.getById(personalTarget.getId());
//            List<String> feedbacks = JSON.parseArray(target.getFeedback(), String.class);
//            feedbacks = CollectionUtil.isEmpty(feedbacks)?new ArrayList<>():feedbacks;
//            feedbacks.add(feedback);
            List<String> feedbacks = new ArrayList<>();
            feedbacks.add(feedback);
            this.update(new LambdaUpdateWrapper<PersonalTarget>().set(PersonalTarget::getFeedback,JSON.toJSONString(feedbacks))
                    .eq(BaseEntity::getId,personalTarget.getId()));
            //确定产品内业
            Long personalId = personalTarget.getPersonalId();
            personalService.fixationInwardStaff(personalId);

        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }


    @Transactional
    @Override
    public Boolean updatePrice(TaskRecordDTO<PersonalTarget> taskRecordDTO) {
        PersonalTarget taskData = taskRecordDTO.getTaskData();
        if (taskData.getId()!=null){
            //把订单表的委托人更新一下
            personalService.update(new LambdaUpdateWrapper<Personal>()
                    .eq(BaseEntity::getId,taskData.getPersonalId()).set(Personal::getLocation,taskData.getLocation())
                    .set(Personal::getBailorA,taskData.getBailorA()).set(Personal::getBailorB,taskData.getBailorB()));

            return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,taskData.getId())
                    .set(PersonalTarget::getAcreage,taskData.getAcreage())
                    .set(PersonalTarget::getPrice,taskData.getPrice())
                    .set(PersonalTarget::getAmount,taskData.getAmount())
                    .set(PersonalTarget::getLocation,taskData.getLocation()));
        }
        return Boolean.FALSE;
    }

    @Transactional
    @Override
    public Boolean houseCertificate(PersonalHouseCertificateVO houseCertificateVO) {
        Long targetId = houseCertificateVO.getId();
        String location = houseCertificateVO.getLocation();
        String bailor1 = houseCertificateVO.getBailor1();
        String bailor2 = houseCertificateVO.getBailor2();
        personalService.updateCertificates(targetId,HOUSE_CERTIFICATE,location,bailor1,bailor2);
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                        .set(PersonalTarget::getLocation,location)
                .set(PersonalTarget::getHouseCertificate,JSON.toJSONString(houseCertificateVO)));
    }

    @Transactional
    @Override
    public Boolean landCertificate(PersonalLandCertificateVO landCertificateVO) {
        Long targetId = landCertificateVO.getId();
        personalService.updateCertificates(targetId,LAND_CERTIFICATE,null,null,null);
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                .set(PersonalTarget::getLandCertificate,JSON.toJSONString(landCertificateVO)));
    }

    @Transactional
    @Override
    public Boolean immovableCertificate(PersonalImmovableCertificateVO immovableCertificateVO) {
        Long targetId = immovableCertificateVO.getId();
        //证件上面的坐落
        String location = immovableCertificateVO.getLocation();
        String bailor1 = immovableCertificateVO.getBailor1();
        String bailor2 = immovableCertificateVO.getBailor2();
        personalService.updateCertificates(targetId,IMMOVABLE_CERTIFICATE,location,bailor1,bailor2);
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                        .set(PersonalTarget::getLocation,location)
                .set(PersonalTarget::getImmovableCertificate,JSON.toJSONString(immovableCertificateVO)));
    }

    @Override
    public Boolean entityInfo(PersonalEntityInfoVO entityInfoVO) {
        Long targetId = entityInfoVO.getId();
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                .set(PersonalTarget::getEntityInfo,JSON.toJSONString(entityInfoVO)));
    }

    @Override
    public Boolean backgroundInfo(PersonalBackgroundInfo backgroundInfo) {
        Long targetId = backgroundInfo.getId();
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                .set(PersonalTarget::getBackgroundInfo,JSON.toJSONString(backgroundInfo)));
    }

    @Override
    public Boolean sellingAbility(PersonalSellingAbilityVO sellingAbilityVO) {
        Long targetId = sellingAbilityVO.getId();
        return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,targetId)
                .set(PersonalTarget::getSellingAbility,JSON.toJSONString(sellingAbilityVO)));
    }

    @Override
    public Boolean doFeedback(TaskRecordDTO<PersonalTarget> taskRecordDTO) {
        PersonalTarget taskData = taskRecordDTO.getTaskData();
        String feedback = taskData.getFeedback();
        if (StrUtil.isNotBlank(feedback)){
            //确定内业人员
            personalService.fixationInwardStaff(taskData.getPersonalId());
            return this.update(new LambdaUpdateWrapper<PersonalTarget>().eq(BaseEntity::getId,taskData.getId()).set(PersonalTarget::getFeedback, feedback));
        }
        return Boolean.FALSE;
    }

    @Override
    public List<PersonalBackgroundInfo> fuzzyBackgroundList(String location) {
        List<String> list = personalTargetMapper.fuzzyBackgroundList(location);
        List<PersonalBackgroundInfo> result = list.stream().map(x -> {
            PersonalBackgroundInfo personalBackgroundInfo = JSON.parseObject(x, PersonalBackgroundInfo.class);
            personalBackgroundInfo.setId(null);
            return personalBackgroundInfo;
        }).collect(Collectors.toList());
        return result;
    }
}
