package com.mortals.xhx.module.api.web;

import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.api.RspCode;
import com.mortals.xhx.module.api.common.ListQueryResultRsp;
import com.mortals.xhx.module.api.common.PageInfoReq;
import com.mortals.xhx.module.api.common.PageInfoRsp;
import com.mortals.xhx.module.api.common.QueryApiReq;
import com.mortals.xhx.module.api.model.*;
import com.mortals.xhx.module.partner.model.PartnerEntity;
import com.mortals.xhx.module.partner.model.PartnerQuery;
import com.mortals.xhx.module.partner.service.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  客户接口
 * @author luoshichao
 * @date 2021/3/1 16:35
 */
@Api(tags = "客户接口")
@RestController
@RequestMapping(value = "api/partner", method = RequestMethod.POST)
public class PartnerApiController extends BaseApiController{

    @Autowired
    private PartnerService partnerService;

    @Autowired
    private ParamService paramService;

    @ApiOperation("获取客户列表")
    @RequestMapping("/list")
    public BaseRsp<ListQueryResultRsp<PartnerInfo>> list(@RequestBody QueryApiReq<PartnerGetReq> params) {
        BaseRsp<ListQueryResultRsp<PartnerInfo>> baseRsp = BaseRsp.buildSuccRsp();
        ListQueryResultRsp<PartnerInfo> resultData = new ListQueryResultRsp<>();

        //字典信息
        Map<String, Object> dict = new HashMap<>();
        dict.put("tags", paramService.getParamByFirstOrganize(Constant.Param_PartnerTags));
        dict.put( "nature",  paramService.getParamByFirstOrganize(Constant.Param_PartnerNature));
        resultData.setDict(dict);
        try {
            List<PartnerInfo> resultList = new ArrayList<>();

            PartnerQuery partnerQuery  = new PartnerQuery();

            PartnerGetReq req = params.getQuery();
            if(StringUtils.isNotEmpty(req.getCode())){
                partnerQuery.setCode("%" + req.getCode() +"%"); //支持模糊查询
            }
            if(StringUtils.isNotEmpty(req.getName())){
                partnerQuery.setName("%" + req.getName() +"%"); //支持模糊查询
            }
            if(params.getOrderCols() != null && !params.getOrderCols().isEmpty()){
                partnerQuery.setOrderCols(params.getOrderCols());
            }
            PageInfoReq pageInfoReq = params.getPageInfo();
            if (pageInfoReq != null && pageInfoReq.getPrePageResult() != 0) {//分页
                PageInfo pageInfo = new PageInfo();
                pageInfo.setCurrPage(pageInfoReq.getCurrPage());
                pageInfo.setPrePageResult(pageInfoReq.getPrePageResult());
                Result<PartnerEntity> partnerEntityResult = partnerService.find(partnerQuery, pageInfo, null);
                //分页信息
                PageInfoRsp pageInfoRsp = new PageInfoRsp();
                PropertyUtils.copyProperties(pageInfoRsp, partnerEntityResult.getPageInfo());
                resultData.setPageInfo(pageInfoRsp);

                List<PartnerEntity> list = partnerEntityResult.getList();
                if(!list.isEmpty()){
                    resultList = list.stream().map(item->{
                        PartnerInfo info = new PartnerInfo();
                        BeanUtils.copyProperties(item, info);
                        return info;
                    }).collect(Collectors.toList());
                }
            }else{
                List<PartnerEntity> list = partnerService.find(partnerQuery, null);
                resultList = list.stream().map(item->{
                    PartnerInfo info = new PartnerInfo();
                    BeanUtils.copyProperties(item, info);
                    return info;
                }).collect(Collectors.toList());
            }
            resultData.setList(resultList);
            baseRsp.setData(resultData);
        } catch (Exception e) {
            log.error("", e);
            baseRsp = BaseRsp.buildFailRsp(RspCode.UNKNOWN_ERROR.getValue(), super.convertException(e));
        }
        return baseRsp;
    }
}
