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

import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.YesNo;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.util.StringUtils;
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.warehouse.model.WarehouseEntity;
import com.mortals.xhx.module.warehouse.model.WarehouseQuery;
import com.mortals.xhx.module.warehouse.service.WarehouseService;
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/warehouse", method = RequestMethod.POST)
public class WarehouseApiController extends BaseApiController{

    @Autowired
    private WarehouseService warehouseService;

    @ApiOperation("获取供应商列表")
    @RequestMapping("/list")
    public BaseRsp<ListQueryResultRsp<WarehouseInfo>> list(@RequestBody QueryApiReq<WarehouseGetReq> params) {
        BaseRsp<ListQueryResultRsp<WarehouseInfo>> baseRsp = BaseRsp.buildSuccRsp();
        ListQueryResultRsp<WarehouseInfo> resultData = new ListQueryResultRsp<>();

        //字典信息
        Map<String, Object> dict = new HashMap<>();
        dict.put("status", IBaseEnum.getEnumMap(YesNo.class));
        resultData.setDict(dict);

        try {
            List<WarehouseInfo> resultList = new ArrayList<>();

            WarehouseQuery warehouseQuery  = new WarehouseQuery();
            WarehouseGetReq req = params.getQuery();
            if(StringUtils.isNotEmpty(req.getCode())){
                warehouseQuery.setCode("%" + req.getCode() +"%"); //支持模糊查询
            }
            if(StringUtils.isNotEmpty(req.getName())){
                warehouseQuery.setName("%" + req.getName() +"%"); //支持模糊查询
            }
            if(params.getOrderCols() != null && !params.getOrderCols().isEmpty()){
                warehouseQuery.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<WarehouseEntity> partnerEntityResult = warehouseService.find(warehouseQuery, pageInfo, null);
                //分页信息
                PageInfoRsp pageInfoRsp = new PageInfoRsp();
                PropertyUtils.copyProperties(pageInfoRsp, partnerEntityResult.getPageInfo());
                resultData.setPageInfo(pageInfoRsp);

                List<WarehouseEntity> list = partnerEntityResult.getList();
                if(!list.isEmpty()){
                    resultList = list.stream().map(item->{
                        WarehouseInfo info = new WarehouseInfo();
                        BeanUtils.copyProperties(item, info);
                        return info;
                    }).collect(Collectors.toList());
                }
            }else{
                List<WarehouseEntity> list = warehouseService.find(warehouseQuery, null);
                resultList = list.stream().map(item->{
                    WarehouseInfo info = new WarehouseInfo();
                    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;
    }
}
