package com.cool.bang.modules.api.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.util.WeixinUtil;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.Corp;
import com.cool.bang.modules.business.domain.DataCollection;
import com.cool.bang.modules.business.service.ICorpService;
import com.cool.bang.modules.business.service.IDataCollectionService;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 客源
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-21 18:05
 */
@RestController
@RequestMapping(ControllerConstant.WX + "data/v1")
public class CorpDataController extends BaseController {

    @Autowired
    private IDataCollectionService dataCollectionService;

    @Autowired
    private ICorpService corpService;

    /**
     * 根据企业查询所有上报数据
     *
     * @return
     */
    @GetMapping("dataMain")
    public ResultTable dataMain(PageDomain pageDomain, DataCollection param) {
        if (param.getCorpId() == null){
            return pageTable(null,0);
        }
        PageInfo<DataCollection> clfDataCollectionPage = dataCollectionService.page(pageDomain, param);
        return pageTable(clfDataCollectionPage.getList(), clfDataCollectionPage.getTotal());
    }

    /**
     * 根据数据id查询上报数据
     *
     * @return
     */
    @GetMapping("dataView/{id}")
    public Result dataView(@PathVariable Long id) {
        return success(dataCollectionService.queryById(id));
    }

    /**
     * 根据企业id查询企业详情
     *
     * @return
     */
    @GetMapping("corpView/{id}")
    public Result corpView(@PathVariable Long id) {
        return success(corpService.queryById(id));
    }

    /**
     * 填报数据
     *
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody @Validated DataCollection dataCollection) {
        int count = dataCollectionService.queryDataYear(dataCollection.getYearMonth(),dataCollection.getCorpId(),dataCollection.getDistrict());
        if (count > 0){
            return failure("本企业已经上报了当月数据，请勿重复填报！");
        }
        int isEnd = dataCollectionService.queryIsEnd(dataCollection.getYearMonth(),dataCollection.getDistrict());
        dataCollection.setIsEnd(isEnd);
        dataCollection.setCreateTime(new Date());
        dataCollection.setDelFlag(1);
        return decide(dataCollectionService.save(dataCollection));
    }

    /**
     * 修改数据
     *
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody @Validated DataCollection dataCollection) {
        return decide(dataCollectionService.update(dataCollection));
    }


    /**
     * 存量房数据推送
     *
     * @return
     */
    @GetMapping("saleCount")
    public Result saleCount(@RequestBody @Validated DataCollection dataCollection) {
        return decide(dataCollectionService.update(dataCollection));
    }

}
