package com.jpsoft.shinestar.modules.mobile.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.PersonFamilyDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/mobile/personFamilyApi")
public class PersonFamilyApiController {

    @Autowired
    private PersonFamilyService personFamilyService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private CompanyStructureInfoService companyStructureInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @ApiOperation(value="添加家庭成员")
    @PostMapping("add")
    public MessageResult<PersonFamily> add(@RequestBody PersonFamilyDTO personFamilyDto, @RequestAttribute String subject){
        MessageResult<PersonFamily> msgResult = new MessageResult<>();

        try {
            PersonFamily personFamily = new PersonFamily();
            personFamily.setId(UUID.randomUUID().toString());
            personFamily.setDelFlag(false);
            personFamily.setCreateBy(subject);
            personFamily.setCreateTime(new Date());
            personFamily.setName(personFamilyDto.getName());
            personFamily.setPhone(personFamilyDto.getPhone());
            personFamily.setIdCard(personFamilyDto.getIdCard());
            personFamily.setFaceImageUrl(personFamilyDto.getFaceImageUrl());
            personFamily.setType(personFamilyDto.getType());

            //获取身份ID
            PersonInfo personInfo = personInfoService.get(Long.parseLong(subject));
            personFamily.setPopedomId(personInfo.getPopedom());

            int affectCount = personFamilyService.insert(personFamily);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(personFamily);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取家庭成员详情")
    @GetMapping("detail")
    public MessageResult<PersonFamilyDTO> detail(String id,String token, @RequestAttribute String subject){
        MessageResult<PersonFamilyDTO> msgResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.get(Long.parseLong(subject));

            PersonFamilyDTO dto = new PersonFamilyDTO();

            if(StringUtils.isNotEmpty(id)) {

                PersonFamily personFamily = personFamilyService.get(id);

                    if (personFamily != null) {
                        dto.setId(personFamily.getId());
                        dto.setName(personFamily.getName());
                        dto.setPhone(personFamily.getPhone());
                        dto.setFaceImageUrl(personFamily.getFaceImageUrl());
                        dto.setIdCard(personFamily.getIdCard());
                }
            }

            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());

            if (personPopedom != null) {
                CompanyStructureInfo companyStructureInfo = companyStructureInfoService.get(personPopedom.getCompanyStructureId());
                if(companyStructureInfo!=null) {
                    dto.setStructureName(companyStructureInfo.getStructureName());
                }

                CompanyInfo companyInfo = companyInfoService.get(personPopedom.getCompanyId());
                if(companyInfo!=null){
                    dto.setCompanyName(companyInfo.getName());
                }
            }

            msgResult.setResult(true);
            msgResult.setData(dto);

        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="删除家庭成员")
    @PostMapping("delete")
    public MessageResult<PersonFamily> delete(String id,String token, @RequestAttribute String subject){
        MessageResult<PersonFamily> msgResult = new MessageResult<>();

        try {
            PersonFamily personFamily = personFamilyService.get(id);
            personFamily.setDelFlag(true);
            personFamily.setUpdateBy(subject);
            personFamily.setUpdateTime(new Date());

            int affectCount = personFamilyService.update(personFamily);

            if (affectCount > 0) {
                msgResult.setResult(true);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库删除失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value = "类型(0：家庭成员，1：租户)",required = true,paramType = "form"),
            @ApiImplicitParam(name="token",value = "令牌",required = true,paramType = "form"),
            @ApiImplicitParam(name="subject",value = "目标(不传)",paramType = "form")
    })
    public MessageResult<Map> pageList(
            String token,String type,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        PersonInfo personInfo = personInfoService.get(Long.parseLong(subject));

        searchParams.put("popedomId",personInfo.getPopedom());

        searchParams.put("type",type);

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));

        Page<PersonFamily> page = personFamilyService.pageSearch(searchParams,pageIndex,pageSize,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }

}
