package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeejoin.amos.boot.module.jxiop.api.dto.*;
import com.yeejoin.amos.boot.module.jxiop.api.entity.PersonBasic;
import com.yeejoin.amos.boot.module.jxiop.api.entity.*;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.PersonBasicMapper;
import com.yeejoin.amos.boot.module.jxiop.api.service.IPersonBasicService;
import com.yeejoin.amos.boot.module.jxiop.biz.fegin.PrivilegeFeignClient;
import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.feign.privilege.Privilege;
import com.yeejoin.amos.feign.privilege.client.DepartmentClient;
import com.yeejoin.amos.feign.privilege.model.CompanyModel;
import com.yeejoin.amos.feign.privilege.model.DepartmentModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 人员基本信息服务实现类
 *
 * @author system_generator
 * @date 2023-04-20
 */
@Service
public class PersonBasicServiceImpl extends BaseService<PersonBasicDto,PersonBasic,PersonBasicMapper> implements IPersonBasicService {

    @Autowired
    PersonBasicMapper personBasicMapper;

    @Autowired
    PrivilegeFeignClient privilegeFeignClient;

    //人员基本信息service
    PersonBasicServiceImpl personBasicService;
    //人员账号信息service
    PersonAccountServiceImpl personAccountService;
    //人员归属信息service
    PersonAscriptionServiceImpl personAscriptionService;
    //人员资质信息service
    PersonCertificateServiceImpl personCertificateService;
    //人员教育信息
    PersonSkillEducationServiceImpl personSkillEducationService;

    /**
     * 分页查询
     */
    public Page<PersonBasicDto> queryForPersonBasicPage(Page<PersonBasicDto> page)   {
        return this.queryForPage(page, null, false);
    }

    /**
     * 列表查询 示例
     */
    public List<PersonBasicDto> queryForPersonBasicList()   {
        return this.queryForList("" , false);
    }

    /**
     * 新增
     * */
    @Transactional
    public void addPerson(PersonDto model, HttpServletRequest httpServletRequest){
        String appKey = httpServletRequest.getHeader("appKey");
        String product = httpServletRequest.getHeader("product");
        String token = httpServletRequest.getHeader("token");

        //获取人员基本信息数据
        PersonUser personUser =model.getPersonUser();
        //获取人员资质信息
        PersonCertificate personCertificate =model.getPersonCertificate();
        //获取人员账号信息
        PersonAccount personAccount =model.getPersonAccount();

        //人员基础信息
        PersonBasic personBasic = new PersonBasic();
        BeanUtils.copyProperties(personUser,personBasic);
        personBasicService.save(personBasic);
        //人员归属信息
        PersonAscription personAscription = new PersonAscription();
        BeanUtils.copyProperties(personUser,personAscription);
        personAscription.setPersonId(personBasic.getSequenceNbr());
        personAscriptionService.save(personAscription);
        //人员归属信息
        PersonSkillEducation personSkillEducation = new PersonSkillEducation();
        BeanUtils.copyProperties(personUser,personSkillEducation);
        personSkillEducation.setPersonId(personBasic.getSequenceNbr());
        personSkillEducationService.save(personSkillEducation);
        //人员资质信息
        personCertificate.setPersonId(personBasic.getSequenceNbr());
        personCertificateService.save(personCertificate);
        //人员账号信息
        personAccount.setPersonId(personBasic.getSequenceNbr());
        personAccountService.save(personAccount);
    }


    @Transactional
    public PersonDto updatePerson(PersonDto model, HttpServletRequest httpServletRequest) {
        String appKey = httpServletRequest.getHeader("appKey");
        String product = httpServletRequest.getHeader("product");
        String token = httpServletRequest.getHeader("token");

        //获取人员基本信息数据
        PersonUser personUser = model.getPersonUser();
        //获取人员资质信息
        PersonCertificate personCertificate = model.getPersonCertificate();
        //获取人员账号信息
        PersonAccount personAccount = model.getPersonAccount();

        //人员基础信息
        PersonBasic personBasic = new PersonBasic();
        BeanUtils.copyProperties(personUser, personBasic);
        this.saveOrUpdate(personBasic);
        //人员归属信息
        PersonAscription personAscription = new PersonAscription();
        BeanUtils.copyProperties(personUser, personAscription);
        personAscriptionService.saveOrUpdate(personAscription);
        //人员归属信息
        PersonSkillEducation personSkillEducation = new PersonSkillEducation();
        BeanUtils.copyProperties(personUser, personSkillEducation);
        personSkillEducationService.saveOrUpdate(personSkillEducation);
        //人员资质信息
        personCertificateService.saveOrUpdate(personCertificate);
        //人员账号信息
        personAccountService.saveOrUpdate(personAccount);
        return model;
    }

    @Transactional
    public PersonDto getPerson(Long sequenceNbr) {
        PersonDto personDto = new PersonDto();
        PersonUser personUser = new PersonUser();
        QueryWrapper<PersonBasic> wrapper1 = new QueryWrapper();
        wrapper1.eq("sequence_nbr", sequenceNbr);
        wrapper1.eq("is_delete", 0);
        //人员基础信息
        PersonBasic personBasic = this.getOne(wrapper1);
        BeanUtils.copyProperties(personBasic,personUser);

        QueryWrapper<PersonAscription> wrapper2 = new QueryWrapper();
        wrapper2.eq("person_id", sequenceNbr);
        //人员归属信息
        PersonAscription personAscription = personAscriptionService.getOne(wrapper2);
        BeanUtils.copyProperties(personAscription,personUser);

        //人员归属信息
        QueryWrapper<PersonSkillEducation> wrapper3 = new QueryWrapper();
        wrapper3.eq("person_id", sequenceNbr);
        PersonSkillEducation personSkillEducation =personSkillEducationService.getOne(wrapper3);
        BeanUtils.copyProperties(personSkillEducation,personUser);


        QueryWrapper<PersonCertificate> wrapper4 = new QueryWrapper();
        wrapper3.eq("person_id", sequenceNbr);
        //人员资质信息
        PersonCertificate personCertificate = personCertificateService.getOne(wrapper4);


        QueryWrapper<PersonAccount> wrapper5 = new QueryWrapper();
        wrapper3.eq("person_id", sequenceNbr);
        //人员账号信息
        PersonAccount personAccount= personAccountService.getOne(wrapper5);
        personDto.setPersonUser(personUser);
        personDto.setPersonCertificate(personCertificate);
        personDto.setPersonAccount(personAccount);
        return personDto;
    }

    @Transactional
    public int deletePerson(String [] ids) {
        return personBasicMapper.deleteList(ids);
    }

    //新增平台用户
    private ResponseModel<Object> setcreateUser(UserDto userDto, String appKey, String product, String token) {
        return privilegeFeignClient.create(appKey, product, token, userDto);
    }

    //修改平台用户
    private ResponseModel<Object> update(String userId, UserDto userDto, String appKey, String product, String token) {
        return privilegeFeignClient.update(userId, appKey, product, token, userDto);
    }



    /**
     * 分页查询
     */
    public Page<UserMapperDto> queryPage(Page<UserMapperDto> page,
                                           String name,
                                           String accountName,
                                           String projectName)   {

        List<UserMapperDto> list=personBasicMapper.queryPage((page.getCurrent()-1) * page.getSize(),page.getSize(),  name,
                accountName,
                projectName);
        List<UserMapperDto> listcount=personBasicMapper.queryPagecount( name,
                accountName,
                projectName);
        page.setTotal(listcount.size());
        page.setRecords(list);
        return page;
    }





}