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


import com.jpsoft.shinestar.modules.base.dto.CompanyGoodInfoDTO;
import com.jpsoft.shinestar.modules.base.entity.CheckinInfo;
import com.jpsoft.shinestar.modules.base.entity.CompanyGoodInfo;
import com.jpsoft.shinestar.modules.base.entity.CompanyPaymentInfo;
import com.jpsoft.shinestar.modules.base.entity.GoodsInfo;
import com.jpsoft.shinestar.modules.base.service.CheckinInfoService;
import com.jpsoft.shinestar.modules.base.service.CompanyGoodInfoService;
import com.jpsoft.shinestar.modules.base.service.CompanyPaymentInfoService;
import com.jpsoft.shinestar.modules.base.service.GoodsInfoService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/base/companyGoodsInfo")
@Api(description = "companyGoodsInfo")
public class CompanyGoodsInfoController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CompanyGoodInfoService companyGoodInfoService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private CheckinInfoService checkinInfoService;


    @ApiOperation(value="获取商品列表")
    @PostMapping("findByCheckinInfoId")
    public MessageResult<Object> list(String checkinInfoId){
        MessageResult<Object> msgResult = new MessageResult<>();

        try {

            CheckinInfo checkinInfo = checkinInfoService.get(checkinInfoId);

            //String propertyId = checkinInfo.getPropertyId();

            List<CompanyGoodInfo> list = companyGoodInfoService.findByCompanyId(checkinInfo.getId());

            List<CompanyGoodInfoDTO> companyGoodInfoDTOList = new ArrayList<>();

            for (CompanyGoodInfo companyGoodInfo:list) {
                CompanyGoodInfoDTO companyGoodInfoDTO = new CompanyGoodInfoDTO();
                String goodsId = companyGoodInfo.getGoodsId();
                GoodsInfo goodsInfo = goodsInfoService.get(goodsId);
                BeanUtils.copyProperties(companyGoodInfo,companyGoodInfoDTO);

                companyGoodInfoDTO.setGoodsId(goodsId);
                companyGoodInfoDTO.setGoodsName(goodsInfo.getName());
                companyGoodInfoDTO.setGoodTypeId(goodsInfo.getType());

                if (goodsInfo.getIsActivation()) {
                    companyGoodInfoDTOList.add(companyGoodInfoDTO);
                }
            }

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


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

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

        return msgResult;
    }
}
