package com.softwarebr.nunu.web.admin;

import com.alibaba.fastjson.JSON;
import com.brframework.common.utils.ConvertObjectUtil;
import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.EditAlertLayout;
import com.brframework.commoncms.annatotion.layout.EditLayout;
import com.brframework.commoncms.annatotion.layout.PageLayout;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import com.softwarebr.nunu.domain.InviteJob;
import com.softwarebr.nunu.domain.VerifyPageTotal;
import com.softwarebr.nunu.dto.res.InviteParamDTO;
import com.softwarebr.nunu.dto.res.ProjectParamDTO;
import com.softwarebr.nunu.dto.res.PropertyParamDTO;
import com.softwarebr.nunu.dto.res.TalentParamDTO;
import com.softwarebr.nunu.entity.res.*;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.json.admin.res.*;
import com.softwarebr.nunu.service.oper.AreaService;
import com.softwarebr.nunu.service.res.*;
import com.softwarebr.nunu.service.user.MemberService;
import com.softwarebr.nunu.util.AmountConversionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;
import static com.brframework.commondb.core.ControllerPageHandle.jpaPageConvertToPage;

/**
 * @author lilin
 * @date 2020-02-21 11:49:00
 */

@RestController
@Api(tags = "创新资源交易区管理")
@AdminMenu(menuName = "创新资源交易区管理", icon = Icons.TABLE)
@RequestMapping("/admin/access")
public class AdminResController {

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private InviteService inviteService;

    @Autowired
    private TalentService talentService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private PropertyApplyService propertyApplyService;

    @Autowired
    private AreaService areaService;

    @ApiOperation(value = "创新资源交易区状态数据统计", notes = "创新资源交易区状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-query')")
    @GetMapping("/v1/res/count")
    public JSONResult<VerifyPageTotal> getResCount(ProjectQueryParam param1, PropertyQueryParam param2, InviteQueryParam param3, TalentQueryParam param4) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        projectQueryHandle(param1);
        verifyPageResult.setProjectCount(projectService.count(param1));
        propertyQueryHandle(param2);
        verifyPageResult.setPropertyCount(propertyService.count(param2));
        inviteQueryHandle(param3);
        verifyPageResult.setInviteCount(inviteService.count(param3));
        talentQueryHandle(param4);
        verifyPageResult.setTalentCount(talentService.count(param4));
        return JSONResult.ok(verifyPageResult);
    }

    private void projectQueryHandle(ProjectQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        }
        if (null != param.getApplyStatus()) {
            param.setId(propertyApplyService.getAwaitVerifyResourceIds(PropertyApply.RESOURCE_TYPE_PROJECT));
        }
        param.setVerify(SystemConst.VERIFY_ADOPT);
        param.setMarker(SystemConst.MARKER_STATUS_NO);
    }

    private void propertyQueryHandle(PropertyQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        }
        if (null != param.getApplyStatus()) {
            param.setId(propertyApplyService.getAwaitVerifyResourceIds(PropertyApply.RESOURCE_TYPE_PROPERTY));
        }
        param.setVerify(SystemConst.VERIFY_ADOPT);
        param.setMarker(SystemConst.MARKER_STATUS_NO);
    }

    private void inviteQueryHandle(InviteQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        }
        if (null != param.getApplyStatus()) {
            param.setId(propertyApplyService.getAwaitVerifyResourceIds(PropertyApply.RESOURCE_TYPE_INVITE));
        }
        param.setVerify(SystemConst.VERIFY_ADOPT);
        param.setMarker(SystemConst.MARKER_STATUS_NO);
    }


    private void talentQueryHandle(TalentQueryParam param) {
//        if (null != param.getAccount()) {
//            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
//        }
//        if (null != param.getApplyStatus()) {
//            param.setId(propertyApplyService.getAwaitVerifyResourceIds(PropertyApply.RESOURCE_TYPE_INVITE));
//        }
        param.setVerify(SystemConst.VERIFY_ADOPT);
        param.setMarker(SystemConst.MARKER_STATUS_NO);
    }

    @ApiOperation(value = "项目需求资源列表", notes = "项目需求资源列表", produces = "text/plain")
    @GetMapping("/v1/project/page")
    @AdminMenu(menuName = "创新资源交易区管理", order = 1, url = "TransactionReview")
    @PreAuthorize("hasRole('res-project-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "添加", uriMappingMethod = "projectCreateV1")
            },
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getProject"),
                    @CmsOption(name = "上架", uriMappingMethod = "resUpperV1"),
                    @CmsOption(name = "下架", uriMappingMethod = "resLowerV1"),
                    @CmsOption(name = "首页推荐", uriMappingMethod = "resIndexYesV1"),
                    @CmsOption(name = "取消推荐", uriMappingMethod = "resIndexNoV1"),
                    @CmsOption(name = "修改", uriMappingMethod = "projectEditV1"),
                    @CmsOption(name = "公开申请列表", uriMappingMethod = "resApplyListV1"),
                    @CmsOption(name = "公开", uriMappingMethod = "resApplyOpenV1"),
                    @CmsOption(name = "删除", uriMappingMethod = "projectRemoveV1", alert = "确定删除该记录吗?")
            }
    )
    public JSONResult<Page<ProjectResult>> projectListV1(PageParam pageParam, ProjectQueryParam param) {
        projectQueryHandle(param);
        Page<ProjectResult> page = jpaPageConvertToPage(projectService.page(pageParam, param), pageParam, ProjectResult.class);
        Map<Integer, String> statusMap = projectService.statusMap();
        Map<Integer, String> typeMap = projectService.typeMap();
        for (ProjectResult projectResult : page.getList()) {
            if (projectResult.getPublisher() > 0) {
                projectResult.setAccount(memberService.accessObject(projectResult.getPublisher()).getPhone());
            } else {
                projectResult.setAccount("平台");
            }
            projectResult.setPrice(AmountConversionUtil.out(projectResult.getPrice()));
            projectResult.setPriceStr(projectResult.getPrice().longValue() > 0 ? projectResult.getPrice().toString() : "免费");
            projectResult.setVerifyTitle(SystemConst.verifyHandle(projectResult.getVerify()));
            projectResult.setUpperTitle(SystemConst.statusUpperHandle(projectResult.getUpperStatus()));
            projectResult.setPublicityTitle(SystemConst.publicityHandle(projectResult.getPublicity()));
            projectResult.setStatusTitle(statusMap.get(projectResult.getStatus()));
            projectResult.setTypeTitle(typeMap.get(projectResult.getType()));
            projectResult.setHandleTotal(projectResult.getPublicity().equals(SystemConst.PUBLICITY_PUBLIC) ? 0 :
                    propertyApplyService.countByVerifyStatus(PropertyApply.RESOURCE_TYPE_PROJECT, projectResult.getId()).intValue());
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "修改资源信息", notes = "修改资源信息", produces = "text/plain")
    @PostMapping("/v1/project/{id}")
    @PreAuthorize("hasRole('res-project-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getProject"
    )
    public JSONResult projectEditV1(@PathVariable Long id, @Valid @RequestBody ProjectParam param) {
        projectService.updateProject(id, convertTo(param, ProjectParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加资源信息", notes = "添加资源信息", produces = "text/plain")
    @PutMapping("/v1/project/add")
    @PreAuthorize("hasRole('res-project-create-v1')")
    @AOLog
    @EditLayout
    public JSONResult projectCreateV1(@Valid @RequestBody ProjectParam param) {
        projectService.saveProject(-1L, convertTo(param, ProjectParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "删除资源信息", notes = "删除资源信息", produces = "text/plain")
    @DeleteMapping("/v1/project/{id}")
    @PreAuthorize("hasRole('res-project-del-v1')")
    @AOLog
    public JSONResult projectRemoveV1(@PathVariable Long id) {
        projectService.removeProjectById(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "查看资源信息", notes = "查看资源信息", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-query')")
    @GetMapping("/v1/project/{id}")
    public JSONResult<Project> getProject(@PathVariable Long id) {
        Project project = projectService.accessObject(id);
        project.setPrice(AmountConversionUtil.out(project.getPrice()));
        return JSONResult.ok(project);
    }


    @ApiOperation(value = "招聘资源列表", notes = "招聘资源列表", produces = "text/plain")
    @GetMapping("/v1/invite/page")
    @PreAuthorize("hasRole('res-project-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "添加", uriMappingMethod = "inviteCreateV1")
            },
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getInvite"),
                    @CmsOption(name = "上架", uriMappingMethod = "resUpperV1"),
                    @CmsOption(name = "下架", uriMappingMethod = "resLowerV1"),
                    @CmsOption(name = "首页推荐", uriMappingMethod = "resIndexYesV1"),
                    @CmsOption(name = "取消推荐", uriMappingMethod = "resIndexNoV1"),
                    @CmsOption(name = "修改", uriMappingMethod = "inviteEditV1"),
                    @CmsOption(name = "公开申请列表", uriMappingMethod = "resApplyListV1"),
                    @CmsOption(name = "公开", uriMappingMethod = "resApplyOpenV1"),
                    @CmsOption(name = "删除", uriMappingMethod = "inviteDelV1", alert = "确定删除该记录吗?")
            }
    )
    public JSONResult<Page<InviteResult>> inviteListV1(PageParam pageParam, InviteQueryParam param) {
        inviteQueryHandle(param);
        Page<InviteResult> page = jpaPageConvertToPage(inviteService.page(pageParam, param), pageParam, InviteResult.class);
        for (InviteResult inviteResult : page.getList()) {
            if (inviteResult.getPublisher() > 0) {
                inviteResult.setAccount(memberService.accessObject(inviteResult.getPublisher()).getPhone());
            } else {
                inviteResult.setAccount("平台");
            }
            inviteResult.setAddress(areaService.addressSplicing(inviteResult.getProvinceId(), inviteResult.getCityId(), inviteResult.getAreaId()));
            inviteResult.setVerifyTitle(SystemConst.verifyHandle(inviteResult.getVerify()));
            inviteResult.setUpperTitle(SystemConst.statusUpperHandle(inviteResult.getUpperStatus()));
            inviteResult.setPublicityTitle(SystemConst.publicityHandle(inviteResult.getPublicity()));
            inviteResult.setStartEndDate(String.format("%s - %s", inviteResult.getStartDate(), inviteResult.getEndDate()));
            inviteResult.setHandleTotal(inviteResult.getPublicity().equals(SystemConst.PUBLICITY_PUBLIC) ? 0 :
                    propertyApplyService.countByVerifyStatus(PropertyApply.RESOURCE_TYPE_INVITE, inviteResult.getId()).intValue());
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "修改招聘资源信息", notes = "修改招聘资源信息", produces = "text/plain")
    @PostMapping("/v1/invite/{id}")
    @PreAuthorize("hasRole('res-project-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getInvite"
    )
    public JSONResult inviteEditV1(@PathVariable Long id, @Valid @RequestBody InviteParam param) {
        InviteParamDTO inviteParamDTO = convertTo(param, InviteParamDTO.class);
        inviteParamDTO.setJobJson(JSON.toJSONString(param.getJob()));
        inviteService.updateInvite(id, inviteParamDTO);
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加招聘资源信息", notes = "添加招聘资源信息", produces = "text/plain")
    @PutMapping("/v1/invite/add")
    @PreAuthorize("hasRole('res-project-create-v1')")
    @AOLog
    @EditLayout
    public JSONResult inviteCreateV1(@Valid @RequestBody InviteParam param) {
        InviteParamDTO inviteParamDTO = convertTo(param, InviteParamDTO.class);
        inviteParamDTO.setJobJson(JSON.toJSONString(param.getJob()));
        inviteService.saveInvite(-1L, inviteParamDTO);
        return JSONResult.ok();
    }

    @ApiOperation(value = "删除招聘资源信息", notes = "删除招聘资源信息", produces = "text/plain")
    @DeleteMapping("/v1/invite/{id}")
    @PreAuthorize("hasRole('res-project-del-v1')")
    @AOLog
    public JSONResult inviteDelV1(@PathVariable Long id) {
        inviteService.removeInviteById(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "查看招聘资源信息", notes = "查看招聘资源信息", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-query')")
    @GetMapping("/v1/invite/{id}")
    public JSONResult<InviteDetailsResult> getInvite(@PathVariable Long id) {
        Invite invite = inviteService.accessObject(id);
        InviteDetailsResult inviteResult = convertTo(invite, InviteDetailsResult.class);
        inviteResult.setArea(areaService.accessObject(invite.getAreaId()).getCityName());
        inviteResult.setCity(areaService.accessObject(invite.getCityId()).getCityName());
        inviteResult.setProvince(areaService.accessObject(invite.getProvinceId()).getCityName());
        if (null != invite.getJob()) {
            inviteResult.setJobs(JSON.parseArray(invite.getJob(), InviteJob.class));
        }
        return JSONResult.ok(inviteResult);
    }

    @ApiOperation(value = "载体资源列表", notes = "载体资源列表", produces = "text/plain")
    @GetMapping("/v1/property/page")
    @PreAuthorize("hasRole('res-project-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "添加", uriMappingMethod = "propertyCreateV1")
            },
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getProperty"),
                    @CmsOption(name = "上架", uriMappingMethod = "resUpperV1"),
                    @CmsOption(name = "下架", uriMappingMethod = "resLowerV1"),
                    @CmsOption(name = "首页推荐", uriMappingMethod = "resIndexYesV1"),
                    @CmsOption(name = "取消推荐", uriMappingMethod = "resIndexNoV1"),
                    @CmsOption(name = "修改", uriMappingMethod = "propertyEditV1"),
                    @CmsOption(name = "公开申请列表", uriMappingMethod = "resApplyListV1"),
                    @CmsOption(name = "公开", uriMappingMethod = "resApplyOpenV1"),
                    @CmsOption(name = "删除", uriMappingMethod = "propertyDelV1", alert = "确定删除该记录吗?")
            }
    )
    public JSONResult<Page<PropertyResult>> propertyListV1(PageParam pageParam, PropertyQueryParam param) {
        propertyQueryHandle(param);
        Page<PropertyResult> page = jpaPageConvertToPage(propertyService.page(pageParam, param), pageParam, PropertyResult.class);
        for (PropertyResult propertyResult : page.getList()) {
            if (propertyResult.getPublisher() > 0) {
                propertyResult.setAccount(memberService.accessObject(propertyResult.getPublisher()).getPhone());
            } else {
                propertyResult.setAccount("平台");
            }
            propertyResult.setAddress(areaService.addressSplicing(propertyResult.getProvinceId(), propertyResult.getCityId(), propertyResult.getAreaId(), propertyResult.getStreetId()));
            propertyResult.setPrice(AmountConversionUtil.out(propertyResult.getPrice()));
            propertyResult.setPriceStr(propertyResult.getPrice().longValue() > 0 ? propertyResult.getPrice().toString() : "免费");
            propertyResult.setCapacity(String.format("%s - %s", propertyResult.getMinCapacity(), propertyResult.getMaxCapacity()));
            propertyResult.setVerifyTitle(SystemConst.verifyHandle(propertyResult.getVerify()));
            propertyResult.setUpperTitle(SystemConst.statusUpperHandle(propertyResult.getUpperStatus()));
            propertyResult.setPublicityTitle(SystemConst.publicityHandle(propertyResult.getPublicity()));
            propertyResult.setStartEndDate(String.format("%s - %s", propertyResult.getStartDate(), propertyResult.getEndDate()));
            propertyResult.setHandleTotal(propertyResult.getPublicity().equals(SystemConst.PUBLICITY_PUBLIC) ? 0 :
                    propertyApplyService.countByVerifyStatus(PropertyApply.RESOURCE_TYPE_PROPERTY, propertyResult.getId()).intValue());
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "修改载体资源信息", notes = "修改载体资源信息", produces = "text/plain")
    @PostMapping("/v1/property/{id}")
    @PreAuthorize("hasRole('res-project-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getProperty"
    )
    public JSONResult propertyEditV1(@PathVariable Long id, @Valid @RequestBody PropertyParam param) {
        propertyService.updateProperty(id, convertTo(param, PropertyParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加载体资源信息", notes = "添加载体资源信息", produces = "text/plain")
    @PutMapping("/v1/property/add")
    @PreAuthorize("hasRole('res-project-create-v1')")
    @AOLog
    @EditLayout
    public JSONResult propertyCreateV1(@Valid @RequestBody PropertyParam param) {
        propertyService.saveProperty(-1L, convertTo(param, PropertyParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "删除载体资源信息", notes = "删除载体资源信息", produces = "text/plain")
    @DeleteMapping("/v1/property/{id}")
    @PreAuthorize("hasRole('res-project-del-v1')")
    @AOLog
    public JSONResult propertyDelV1(@PathVariable Long id) {
        propertyService.removePropertyById(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "查看载体资源信息", notes = "查看载体资源信息", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-query')")
    @GetMapping("/v1/property/{id}")
    public JSONResult<PropertyDetailsResult> getProperty(@PathVariable Long id) {
        PropertyDetailsResult property = convertTo(propertyService.accessObject(id), PropertyDetailsResult.class);
        property.setArea(areaService.accessObject(property.getAreaId()).getCityName());
        property.setCity(areaService.accessObject(property.getCityId()).getCityName());
        property.setProvince(areaService.accessObject(property.getProvinceId()).getCityName());
        property.setStreet(areaService.accessObject(property.getStreetId()).getCityName());
        property.setPrice(AmountConversionUtil.out(property.getPrice()));
        return JSONResult.ok(property);
    }


    @ApiOperation(value = "交易资源上架", notes = "交易资源上架", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-upper-edit-v1')")
    @PostMapping("/v1/res/upper/{id}/{type}")
    @AOLog
    public JSONResult resUpperV1(@PathVariable Long id,
                                 @ApiParam("类型 1.项目资源2.招聘资源3.载体资源4.人才库简历") @PathVariable int type) {
        switch (type) {
            case 1:
                projectService.projectUpperShelf(id);
                break;
            case 2:
                inviteService.inviteUpperShelf(id);
                break;
            case 3:
                propertyService.propertyUpperShelf(id);
                break;
            case 4:
                talentService.talentUpperShelf(id);
                break;

        }
        return JSONResult.ok();
    }

    @ApiOperation(value = "交易资源下架", notes = "交易资源下架", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-lower-edit-v1')")
    @PostMapping("/v1/res/lower/{id}/{type}")
    @AOLog
    public JSONResult resLowerV1(@PathVariable Long id,
                                 @ApiParam("类型 1.项目资源2.招聘资源3.载体资源4.人才库简历") @PathVariable int type) {
        switch (type) {
            case 1:
                projectService.projectLowerShelf(id);
                break;
            case 2:
                inviteService.inviteLowerShelf(id);
                break;
            case 3:
                propertyService.propertyLowerShelf(id);
                break;
            case 4:
                talentService.talentLowerShelf(id);
                break;
        }
        return JSONResult.ok();
    }

    @ApiOperation(value = "交易资源首页推荐", notes = "交易资源首页推荐", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-index-edit-v1')")
    @PostMapping("/v1/res/index/yes/{id}/{type}")
    @AOLog
    public JSONResult resIndexYesV1(@PathVariable Long id,
                                    @ApiParam("类型 1.项目资源2.招聘资源3.载体资源4.人才库简历") @PathVariable int type) {
        switch (type) {
            case 1:
                projectService.projectIndexYes(id);
                break;
            case 2:
                inviteService.inviteIndexYes(id);
                break;
            case 3:
                propertyService.propertyIndexYes(id);
                break;
            case 4:
                talentService.talentIndexYes(id);
                break;
        }
        return JSONResult.ok();
    }

    @ApiOperation(value = "交易资源取消推荐", notes = "交易资源取消推荐", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-index-cancel-edit-v1')")
    @PostMapping("/v1/res/index/no/{id}/{type}")
    @AOLog
    public JSONResult resIndexNoV1(@PathVariable Long id,
                                   @ApiParam("类型 1.项目资源2.招聘资源3.载体资源4.人才库简历") @PathVariable int type) {
        switch (type) {
            case 1:
                projectService.projectIndexNo(id);
                break;
            case 2:
                inviteService.inviteIndexNo(id);
                break;
            case 3:
                propertyService.propertyIndexNo(id);
                break;
            case 4:
                talentService.talentIndexNo(id);
                break;
        }
        return JSONResult.ok();
    }

    @ApiOperation(value = "资源公开申请列表", notes = "资源公开申请列表", produces = "text/plain")
    @GetMapping("/v1/res/apply/page")
    @PreAuthorize("hasRole('res-apply-query')")
    @PageLayout()
    public JSONResult<Page<PropertyApplyResult>> resApplyListV1(PageParam pageParam, PropertyApplyQueryParam param) {
        if (null != param.getAccount()) {
            param.setProposer(memberService.findMemberIdByPhone(param.getAccount()));
        }
        param.setVerify(SystemConst.VERIFY_AWAIT);
        Page<PropertyApplyResult> page = jpaPageConvertToPage(propertyApplyService.page(pageParam, param), pageParam, PropertyApplyResult.class);
        for (PropertyApplyResult result : page.getList()) {
            result.setAccount(memberService.accessObject(result.getProposer()).getPhone());
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "交易资源公开", notes = "交易资源公开", produces = "text/plain")
    @PreAuthorize("hasRole('res-apply-open-edit-v1')")
    @PostMapping("/v1/res/apply/open")
    @AOLog
    public JSONResult resApplyOpenV1(@Valid @RequestBody PropertyApplyParam param) {
        propertyApplyService.openPropertyApply(Arrays.asList(param.getId()));
        return JSONResult.ok();
    }

    @ApiOperation(value = "交易资源审核", notes = "交易资源审核", produces = "text/plain")
    @PreAuthorize("hasRole('res-verify-edit-v1')")
    @PostMapping("/v1/res/verify")
    @AOLog
    public JSONResult resVerifyV1(@Valid @RequestBody VerifyParam param) {
        switch (param.getType()) {
            case 1:
                projectService.projectVerify(param.getId(), param.getVerify(), param.getVerifyRemark());
                break;
            case 2:
                inviteService.inviteVerify(param.getId(), param.getVerify(), param.getVerifyRemark());
                break;
            case 3:
                propertyService.propertyVerify(param.getId(), param.getVerify(), param.getVerifyRemark());
                break;
            case 4:
                talentService.talentVerify(param.getId(), param.getVerify(), param.getVerifyRemark());
                break;
        }
        return JSONResult.ok();
    }

    @ApiOperation(value = "项目需求资源审核列表", notes = "项目需求资源审核列表", produces = "text/plain")
    @GetMapping("/v1/project/verify/page")
    @AdminMenu(menuName = "创新资源交易区审核管理", order = 1, url = "ResourceAudit")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @PageLayout(
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getProject"),
                    @CmsOption(name = "审核", uriMappingMethod = "resVerifyV1")
            }
    )
    public JSONResult<Page<ProjectResult>> projectVerifyListV1(PageParam pageParam, ProjectQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        Page<ProjectResult> page = jpaPageConvertToPage(projectService.page(pageParam, param), pageParam, ProjectResult.class);
        for (ProjectResult projectResult : page.getList()) {
            if (projectResult.getPublisher() > 0) {
                projectResult.setAccount(memberService.accessObject(projectResult.getPublisher()).getPhone());
            }
            projectResult.setPrice(AmountConversionUtil.out(projectResult.getPrice()));
        }
        return JSONResult.ok(page);
    }


    @ApiOperation(value = "项目需求资源审核状态数据统计", notes = "项目需求资源审核状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @GetMapping("/v1/project/verify/count")
    public JSONResult<VerifyPageTotal> getProjectVerifyCount(ProjectQueryParam param) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        verifyPageResult.setTotalCount(projectService.count(param));
        param.setVerify(SystemConst.VERIFY_AWAIT);
        verifyPageResult.setVerifyAwaitCount(projectService.count(param));
        param.setVerify(SystemConst.VERIFY_ADOPT);
        verifyPageResult.setVerifyAdoptCount(projectService.count(param));
        param.setVerify(SystemConst.VERIFY_REJECT);
        verifyPageResult.setVerifyRejectCount(projectService.count(param));
        return JSONResult.ok(verifyPageResult);
    }


    @ApiOperation(value = "载体资源审核列表", notes = "载体资源审核列表", produces = "text/plain")
    @GetMapping("/v1/property/verify/page")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @PageLayout(
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getProperty"),
                    @CmsOption(name = "审核", uriMappingMethod = "resVerifyV1")
            }
    )
    public JSONResult<Page<PropertyResult>> propertyVerifyListV1(PageParam pageParam, PropertyQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        Page<PropertyResult> page = jpaPageConvertToPage(propertyService.page(pageParam, param), pageParam, PropertyResult.class);
        for (PropertyResult propertyResult : page.getList()) {
            if (propertyResult.getPublisher() > 0) {
                propertyResult.setAccount(memberService.accessObject(propertyResult.getPublisher()).getPhone());
            }
            propertyResult.setPrice(AmountConversionUtil.out(propertyResult.getPrice()));
            propertyResult.setAddress(areaService.addressSplicing(propertyResult.getProvinceId(), propertyResult.getCityId(), propertyResult.getAreaId()));
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "载体资源审核状态数据统计", notes = "载体资源审核状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @GetMapping("/v1/property/verify/count")
    public JSONResult<VerifyPageTotal> getPropertyVerifyCount(PropertyQueryParam param) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        verifyPageResult.setTotalCount(propertyService.count(param));
        param.setVerify(SystemConst.VERIFY_AWAIT);
        verifyPageResult.setVerifyAwaitCount(propertyService.count(param));
        param.setVerify(SystemConst.VERIFY_ADOPT);
        verifyPageResult.setVerifyAdoptCount(propertyService.count(param));
        param.setVerify(SystemConst.VERIFY_REJECT);
        verifyPageResult.setVerifyRejectCount(propertyService.count(param));
        return JSONResult.ok(verifyPageResult);
    }

    @ApiOperation(value = "招聘资源审核列表", notes = "招聘资源审核列表", produces = "text/plain")
    @GetMapping("/v1/invite/verify/page")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @PageLayout(
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getInvite"),
                    @CmsOption(name = "审核", uriMappingMethod = "resVerifyV1")
            }
    )
    public JSONResult<Page<InviteResult>> inviteVerifyListV1(PageParam pageParam, InviteQueryParam param) {
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        Page<InviteResult> page = jpaPageConvertToPage(inviteService.page(pageParam, param), pageParam, InviteResult.class);
        for (InviteResult inviteResult : page.getList()) {
            if (inviteResult.getPublisher() > 0) {
                inviteResult.setAccount(memberService.accessObject(inviteResult.getPublisher()).getPhone());
            }
            inviteResult.setAddress(areaService.addressSplicing(inviteResult.getProvinceId(), inviteResult.getCityId(), inviteResult.getAreaId()));

        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "招聘资源审核状态数据统计", notes = "招聘资源审核状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @GetMapping("/v1/invite/verify/count")
    public JSONResult<VerifyPageTotal> getInviteVerifyCount(InviteQueryParam param) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        if (null != param.getAccount()) {
            param.setPublisher(memberService.findMemberIdByPhone(param.getAccount()));
        } else {
            param.setPublisher(0L);
        }
        param.setMarker(SystemConst.MARKER_STATUS_NO);
        verifyPageResult.setTotalCount(inviteService.count(param));
        param.setVerify(SystemConst.VERIFY_AWAIT);
        verifyPageResult.setVerifyAwaitCount(inviteService.count(param));
        param.setVerify(SystemConst.VERIFY_ADOPT);
        verifyPageResult.setVerifyAdoptCount(inviteService.count(param));
        param.setVerify(SystemConst.VERIFY_REJECT);
        verifyPageResult.setVerifyRejectCount(inviteService.count(param));
        return JSONResult.ok(verifyPageResult);
    }


    @ApiOperation(value = "人才库简历审核列表", notes = "人才库简历审核列表", produces = "text/plain")
    @GetMapping("/v1/talent/verify/page")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @PageLayout(
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getTalent"),
                    @CmsOption(name = "审核", uriMappingMethod = "resVerifyV1")
            }
    )
    public JSONResult<Page<TalentResult>> talentVerifyListV1(PageParam pageParam, TalentQueryParam param) {
        Page<TalentResult> page = jpaPageConvertToPage(talentService.page(pageParam, param), pageParam, TalentResult.class);
        for (TalentResult inviteResult : page.getList()) {
            if (inviteResult.getPublisher() > 0) {
                inviteResult.setAccount(memberService.accessObject(inviteResult.getPublisher()).getPhone());
            }
            if (inviteResult.getPublisher() > 0) {
                inviteResult.setAccount(memberService.accessObject(inviteResult.getPublisher()).getPhone());
            } else {
                inviteResult.setAccount("平台");
            }
            inviteResult.setVerifyTitle(SystemConst.verifyHandle(inviteResult.getVerify()));
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "人才库简历审核状态数据统计", notes = "人才库简历审核状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-verify-query')")
    @GetMapping("/v1/talent/verify/count")
    public JSONResult<VerifyPageTotal> geTalentVerifyCount(TalentQueryParam param) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        verifyPageResult.setTotalCount(talentService.count(param));
        param.setVerify(SystemConst.VERIFY_AWAIT);
        verifyPageResult.setVerifyAwaitCount(talentService.count(param));
        param.setVerify(SystemConst.VERIFY_ADOPT);
        verifyPageResult.setVerifyAdoptCount(talentService.count(param));
        param.setVerify(SystemConst.VERIFY_REJECT);
        verifyPageResult.setVerifyRejectCount(talentService.count(param));
        return JSONResult.ok(verifyPageResult);
    }




    @ApiOperation(value = "人才库列表", notes = "人才库列表", produces = "text/plain")
    @GetMapping("/v1/talent/page")
    @PreAuthorize("hasRole('res-project-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "发布简历", uriMappingMethod = "talentCreateV1")
            },
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getTalent"),
                    @CmsOption(name = "上架", uriMappingMethod = "resUpperV1"),
                    @CmsOption(name = "下架", uriMappingMethod = "resLowerV1"),
                    @CmsOption(name = "首页推荐", uriMappingMethod = "resIndexYesV1"),
                    @CmsOption(name = "取消推荐", uriMappingMethod = "resIndexNoV1"),
                    @CmsOption(name = "修改", uriMappingMethod = "talentEditV1"),
                    @CmsOption(name = "删除", uriMappingMethod = "talentDelV1", alert = "确定删除该记录吗?")
            }
    )
    public JSONResult<Page<TalentResult>> talentListV1(PageParam pageParam, TalentQueryParam param) {
        talentQueryHandle(param);
        Page<TalentResult> page = jpaPageConvertToPage(talentService.page(pageParam, param), pageParam, TalentResult.class);
        for (TalentResult talentResult : page.getList()) {
            if (talentResult.getPublisher() > 0) {
                talentResult.setAccount(memberService.accessObject(talentResult.getPublisher()).getPhone());
            } else {
                talentResult.setAccount("平台");
            }
            talentResult.setVerifyTitle(SystemConst.verifyHandle(talentResult.getVerify()));
            talentResult.setUpperTitle(SystemConst.statusUpperHandle(talentResult.getUpperStatus()));
            talentResult.setPublicityTitle(SystemConst.publicityHandle(talentResult.getPublicity()));
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "修改简历信息", notes = "修改简历信息", produces = "text/plain")
    @PostMapping("/v1/talent/{id}")
    @PreAuthorize("hasRole('res-project-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getTalent"
    )
    public JSONResult talentEditV1(@PathVariable Long id, @Valid @RequestBody TalentParam param) {
        TalentParamDTO talentParamDTO = convertTo(param, TalentParamDTO.class);
        talentService.updateTalent(id, talentParamDTO);
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加简历信息", notes = "添加简历信息", produces = "text/plain")
    @PutMapping("/v1/talent/add")
    @PreAuthorize("hasRole('res-project-create-v1')")
    @AOLog
    public JSONResult talentCreateV1(@RequestBody TalentParam param) {
        TalentParamDTO talentParamDTO = convertTo(param, TalentParamDTO.class);
        talentService.saveTalent(-1L, talentParamDTO);
        return JSONResult.ok();
    }

    @ApiOperation(value = "删除简历信息", notes = "删除简历信息", produces = "text/plain")
    @DeleteMapping("/v1/talent/{id}")
    @PreAuthorize("hasRole('res-project-del-v1')")
    @AOLog
    public JSONResult talentDelV1(@PathVariable Long id) {
        talentService.removeById(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "查看简历信息", notes = "查看简历信息", produces = "text/plain")
    @PreAuthorize("hasRole('res-project-query')")
    @GetMapping("/v1/talent/{id}")
    public JSONResult<TalentDetailsResult> getTalent(@PathVariable Long id) {
        Talent talent = talentService.accessObject(id);
        TalentDetailsResult talentDetailsResult = convertTo(talent, TalentDetailsResult.class);
        talentDetailsResult.setArea(areaService.accessObject(talent.getAreaId()).getCityName());
        talentDetailsResult.setCity(areaService.accessObject(talent.getCityId()).getCityName());
        talentDetailsResult.setProvince(areaService.accessObject(talent.getProvinceId()).getCityName());

        return JSONResult.ok(talentDetailsResult);
    }

}