package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.BillsDto;
import com.sptg.estate.api.dto.CarDto;
import com.sptg.estate.api.entity.Bills;
import com.sptg.estate.api.entity.BillsItem;
import com.sptg.estate.api.entity.Car;
import com.sptg.estate.api.feigns.BillsFeign;
import com.sptg.estate.api.vo.BillsVO;
import com.sptg.estate.biz.service.IBillsItemService;
import com.sptg.estate.biz.service.IBillsService;
import com.sptg.estate.biz.service.ResidentialServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import lombok.RequiredArgsConstructor;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lianghanqiang
 * @since 2021-01-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bills")
public class BillsController implements BillsFeign {
    @Autowired
    private IBillsService billsService;
    @Autowired
    private IBillsItemService billsItemService;


    /**
     *  获取账单列表
     * */
    @Override
    @PostMapping("/list")
    public Page<Bills> list(
            @RequestParam(value = "current",required = false) Integer current,
            @RequestParam(value = "size",required = false) Integer size,
            Bills bills
    ){
        Page page = new Page();
        if(current!=null){
            page.setCurrent(current);
        }
        if(size!=null){
            page.setSize(size);
        }
        String agencyName = bills.getAgencyName();
        String residentialName = bills.getResidentialName();
        String buildingName = bills.getBuildingName();
        String unitName = bills.getUnitName();
        String floorName = bills.getFloorName();
        String roomName = bills.getRoomName();
        Integer status = bills.getStatus();

        bills.setAgencyName(null);
        bills.setResidentialName(null);
        bills.setBuildingName(null);
        bills.setFloorName(null);
        bills.setUnitName(null);
        bills.setRoomName(null);
        bills.setStatus(null);
        return billsService.selectPage(page,new EntityWrapper<>(bills)
        .like(agencyName!=null,"agency_name",agencyName)
        .like(residentialName!=null,"residential_name",residentialName)
        .like(buildingName!=null,"building_name",buildingName)
        .like(unitName!=null,"unit_name",unitName)
        .like(floorName!=null,"floor_name",floorName)
        .like(roomName!=null,"room_name",roomName)
        .eq(status!=null,"status",status)
        .eq("is_del",0)
        );
    }

    /**
     * 分页
     */
    @Override
    @PostMapping(value = "/page")
    public ResultBean<Page<Bills>> page(@RequestBody BillsDto dto) {
//        Page<Bills> page = dto.getPage();
//        if(page == null){
//            page = new Page<>();
//            page.setSize(10);
//            page.setCurrent(1);
//        }
        return this.billsService.page(dto);
    }

    /**
     *  获取账单详情
     * */
    @Override
    @GetMapping("/detail")
    public BillsVO detail(@RequestParam(value = "id",required = true) Long id) {
        Bills bills = billsService.selectById(id);
        if(bills!=null){
            BillsVO billsVO = new BillsVO();
            BeanUtils.copyProperties(bills,billsVO);
            billsVO.setItemList(billsItemService.selectList(new EntityWrapper<>(new BillsItem()).eq("pid",id)));
            return billsVO;
        }else{
            return null;
        }
    }

    /**
     *  根据id删除账单
     * */
    @Override
    @PostMapping("/delete")
    public  ResultBean<Boolean> delete(@RequestBody BillsDto billsDto) {
        return billsService.delete(billsDto);
    }

    /**
     *  根据id更新账单
     *
     * @return*/
    @Override
    @PostMapping("/edit")
    public ResultBean<Boolean> edit(@RequestBody BillsDto billsDto) {
        System.out.println(billsDto);
         BillsItem billsItem = new BillsItem();
         billsItem.setPid(billsDto.getId());
         billsItem.setItemName(billsDto.getItemName());
         billsItem.setItemDesc(billsDto.getItemDesc());
         billsItem.setItemFee(billsDto.getItemFee());
         billsItemService.update(billsItem,new EntityWrapper<>(new BillsItem()).eq("pid",billsDto.getId()));
        return billsService.edit(billsDto);
    }

    /**
     * 添加账单
     * @return
     */
    @Override
    @PostMapping("/add")
    public ResultBean<Boolean> add(@RequestBody BillsDto billsDto) {
        System.out.println(billsDto);
        BillsItem billsItem = new BillsItem();
        billsItem.setItemName(billsDto.getItemName());
        billsItem.setItemDesc(billsDto.getItemDesc());
        billsItem.setItemFee(billsDto.getItemFee());
        //写入再获取id
        ResultBean<Boolean> resultBean = billsService.add(billsDto);
        ResultBean<Boolean> resultBean1 = new ResultBean<>();
        billsItem.setPid(billsDto.getId());
        if (resultBean.getData()){
            resultBean1.setData(true);
        }
        billsItemService.insert(billsItem);
        return resultBean1;
    }
}

